home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / wgdb-42.lha / wgdb-4.2 / readline / readline.c < prev    next >
C/C++ Source or Header  |  1992-09-11  |  133KB  |  5,641 lines

  1. /* readline.c -- a general facility for reading lines of input
  2.    with emacs style editing and completion. */
  3.  
  4. /* Copyright (C) 1987,1989 Free Software Foundation, Inc.
  5.  
  6.    This file contains the Readline Library (the Library), a set of
  7.    routines for providing Emacs style line input to programs that ask
  8.    for it.
  9.  
  10.    The Library is free software; you can redistribute it and/or modify
  11.    it under the terms of the GNU General Public License as published by
  12.    the Free Software Foundation; either version 1, or (at your option)
  13.    any later version.
  14.  
  15.    The Library is distributed in the hope that it will be useful, but
  16.    WITHOUT ANY WARRANTY; without even the implied warranty of
  17.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18.    General Public License for more details.
  19.  
  20.    The GNU General Public License is often shipped with GNU software, and
  21.    is generally kept in a file called COPYING or LICENSE.  If you do not
  22.    have a copy of the license, write to the Free Software Foundation,
  23.    675 Mass Ave, Cambridge, MA 02139, USA. */
  24.  
  25. /* Remove these declarations when we have a complete libgnu.a. */
  26. #define STATIC_MALLOC
  27. #ifndef STATIC_MALLOC
  28. extern char *xmalloc (), *xrealloc ();
  29. #else
  30. static char *xmalloc (), *xrealloc ();
  31. #endif
  32.  
  33. #include <stdio.h>
  34. #include <sys/types.h>
  35. #include <fcntl.h>
  36. #include <sys/file.h>
  37. #include <signal.h>
  38.  
  39. #ifdef __GNUC__
  40. #define alloca __builtin_alloca
  41. #else
  42. #if defined (sparc) && defined (sun)
  43. #include <alloca.h>
  44. #endif
  45. #endif
  46.  
  47. #define NEW_TTY_DRIVER
  48. #if defined (SYSV) || defined (hpux)  || defined (Xenix)
  49. #undef NEW_TTY_DRIVER
  50. #include <termio.h>
  51. #else
  52. #include <sgtty.h>
  53. #endif
  54.  
  55. #include <errno.h>
  56. extern int errno;
  57.  
  58. #include <setjmp.h>
  59.  
  60. /* These next are for filename completion.  Perhaps this belongs
  61.    in a different place. */
  62. #include <sys/stat.h>
  63.  
  64. #include <pwd.h>
  65. #ifdef SYSV
  66. struct passwd *getpwuid (), *getpwent ();
  67. #endif
  68.  
  69. #define HACK_TERMCAP_MOTION
  70.  
  71. #ifndef SYSV
  72. #include <sys/dir.h>
  73. #else  /* SYSV */
  74. #if defined (Xenix)
  75. #include <sys/ndir.h>
  76. #else
  77. #ifdef hpux
  78. #include <ndir.h>
  79. #else
  80. #include <dirent.h>
  81. #define direct dirent
  82. #define d_namlen d_reclen
  83. #endif  /* hpux */
  84. #endif  /* xenix */
  85. #endif  /* SYSV */
  86.  
  87. /* Some standard library routines. */
  88. #include "readline.h"
  89. #include "history.h"
  90.  
  91. #ifndef digit
  92. #define digit(c)  ((c) >= '0' && (c) <= '9')
  93. #endif
  94.  
  95. #ifndef isletter
  96. #define isletter(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
  97. #endif
  98.  
  99. #ifndef digit_value
  100. #define digit_value(c) ((c) - '0')
  101. #endif
  102.  
  103. #ifndef member
  104. char *index ();
  105. #define member(c, s) ((c) ? index ((s), (c)) : 0)
  106. #endif
  107.  
  108. #ifndef isident
  109. #define isident(c) ((isletter(c) || digit(c) || c == '_'))
  110. #endif
  111.  
  112. #ifndef exchange
  113. #define exchange(x, y) {int temp = x; x = y; y = temp;}
  114. #endif
  115.  
  116. static update_line ();
  117. static void output_character_function ();
  118. static delete_chars ();
  119. static delete_chars ();
  120. static insert_some_chars ();
  121.  
  122. #ifdef VOID_SIGHANDLER
  123. #define sighandler void
  124. #else
  125. #define sighandler int
  126. #endif
  127.  
  128. /* This typedef is equivalant to the one for Function; it allows us
  129.    to say SigHandler *foo = signal (SIGKILL, SIG_IGN); */
  130. typedef sighandler SigHandler ();
  131.  
  132. /* If on, then readline handles signals in a way that doesn't screw. */
  133. #define HANDLE_SIGNALS
  134.  
  135.  
  136. /* **************************************************************** */
  137. /*                                    */
  138. /*            Line editing input utility            */
  139. /*                                    */
  140. /* **************************************************************** */
  141.  
  142. /* A pointer to the keymap that is currently in use.
  143.    By default, it is the standard emacs keymap. */
  144. Keymap keymap = emacs_standard_keymap;
  145.  
  146. #define vi_mode 0
  147. #define emacs_mode 1
  148.  
  149. /* The current style of editing. */
  150. int rl_editing_mode = emacs_mode;
  151.  
  152. /* Non-zero if the previous command was a kill command. */
  153. static int last_command_was_kill = 0;
  154.  
  155. /* The current value of the numeric argument specified by the user. */
  156. int rl_numeric_arg = 1;
  157.  
  158. /* Non-zero if an argument was typed. */
  159. int rl_explicit_arg = 0;
  160.  
  161. /* Temporary value used while generating the argument. */
  162. static int arg_sign = 1;
  163.  
  164. /* Non-zero means we have been called at least once before. */
  165. static int rl_initialized = 0;
  166.  
  167. /* If non-zero, this program is running in an EMACS buffer. */
  168. static char *running_in_emacs = (char *)NULL;
  169.  
  170. /* The current offset in the current input line. */
  171. int rl_point;
  172.  
  173. /* Mark in the current input line. */
  174. int rl_mark;
  175.  
  176. /* Length of the current input line. */
  177. int rl_end;
  178.  
  179. /* Make this non-zero to return the current input_line. */
  180. int rl_done;
  181.  
  182. /* The last function executed by readline. */
  183. Function *rl_last_func = (Function *)NULL;
  184.  
  185. /* Top level environment for readline_internal (). */
  186. static jmp_buf readline_top_level;
  187.  
  188. /* The streams we interact with. */
  189. static FILE *in_stream, *out_stream;
  190.  
  191. /* The names of the streams that we do input and output to. */
  192. FILE *rl_instream = stdin, *rl_outstream = stdout;
  193.  
  194. /* Non-zero means echo characters as they are read. */
  195. int readline_echoing_p = 1;
  196.  
  197. /* Current prompt. */
  198. char *rl_prompt;
  199.  
  200. /* The number of characters read in order to type this complete command. */
  201. int rl_key_sequence_length = 0;
  202.  
  203. /* If non-zero, then this is the address of a function to call just
  204.    before readline_internal () prints the first prompt. */
  205. Function *rl_startup_hook = (Function *)NULL;
  206.  
  207. /* If non-zero, then this is the address of a function to call when
  208.    completing on a directory name.  The function is called with
  209.    the address of a string (the current directory name) as an arg. */
  210. Function *rl_symbolic_link_hook = (Function *)NULL;
  211.  
  212. /* What we use internally.  You should always refer to RL_LINE_BUFFER. */
  213. static char *the_line;
  214.  
  215. /* The character that can generate an EOF.  Really read from
  216.    the terminal driver... just defaulted here. */
  217. static int eof_char = CTRL ('D');
  218.  
  219. /* Non-zero makes this the next keystroke to read. */
  220. int rl_pending_input = 0;
  221.  
  222. /* Pointer to a useful terminal name. */
  223. char *rl_terminal_name = (char *)NULL;
  224.  
  225. /* Line buffer and maintenence. */
  226. char *rl_line_buffer = (char *)NULL;
  227. static int rl_line_buffer_len = 0;
  228. #define DEFAULT_BUFFER_SIZE 256
  229.  
  230.  
  231. /* **************************************************************** */
  232. /*                                    */
  233. /*            `Forward' declarations              */
  234. /*                                    */
  235. /* **************************************************************** */
  236.  
  237. /* Non-zero means do not parse any lines other than comments and
  238.    parser directives. */
  239. static unsigned char parsing_conditionalized_out = 0;
  240.  
  241. /* Caseless strcmp (). */
  242. static int stricmp (), strnicmp ();
  243.  
  244. /* Non-zero means to save keys that we dispatch on in a kbd macro. */
  245. static int defining_kbd_macro = 0;
  246.  
  247.  
  248. /* **************************************************************** */
  249. /*                                    */
  250. /*            Top Level Functions                */
  251. /*                                    */
  252. /* **************************************************************** */
  253.  
  254. /* Read a line of input.  Prompt with PROMPT.  A NULL PROMPT means
  255.    none.  A return value of NULL means that EOF was encountered. */
  256. char *
  257. readline (prompt)
  258.      char *prompt;
  259. {
  260.   static rl_prep_terminal (), rl_deprep_terminal ();
  261.   char *readline_internal ();
  262.   char *value;
  263.  
  264.   rl_prompt = prompt;
  265.  
  266.   /* If we are at EOF return a NULL string. */
  267.   if (rl_pending_input == EOF)
  268.     {
  269.       rl_pending_input = 0;
  270.       return ((char *)NULL);
  271.     }
  272.  
  273.   rl_initialize ();
  274.   rl_prep_terminal ();
  275.  
  276. #ifdef HANDLE_SIGNALS
  277.   rl_set_signals ();
  278. #endif
  279.  
  280.   value = readline_internal ();
  281.   rl_deprep_terminal ();
  282.  
  283. #ifdef HANDLE_SIGNALS
  284.   rl_clear_signals ();
  285. #endif
  286.  
  287.   return (value);
  288. }
  289.  
  290. /* Read a line of input from the global rl_instream, doing output on
  291.    the global rl_outstream.
  292.    If rl_prompt is non-null, then that is our prompt. */
  293. char *
  294. readline_internal ()
  295. {
  296.   int lastc, c, eof_found;
  297.  
  298.   in_stream = rl_instream; out_stream = rl_outstream;
  299.   lastc = eof_found = 0;
  300.  
  301.   if (rl_startup_hook)
  302.     (*rl_startup_hook) ();
  303.  
  304.   if (!readline_echoing_p)
  305.     {
  306.       if (rl_prompt)
  307.     {
  308.       fprintf (out_stream, "%s", rl_prompt);
  309.       fflush (out_stream);
  310.     }
  311.     }
  312.   else
  313.     {
  314.       rl_on_new_line ();
  315.       rl_redisplay ();
  316. #ifdef VI_MODE
  317.       if (rl_editing_mode == vi_mode)
  318.     rl_vi_insertion_mode ();
  319. #endif /* VI_MODE */
  320.     }
  321.  
  322.   while (!rl_done)
  323.     {
  324.       int lk = last_command_was_kill;
  325.       int code = setjmp (readline_top_level);
  326.  
  327.       if (code)
  328.     rl_redisplay ();
  329.  
  330.       if (!rl_pending_input)
  331.     {
  332.       /* Then initialize the argument and number of keys read. */
  333.       rl_init_argument ();
  334.       rl_key_sequence_length = 0;
  335.     }
  336.  
  337.       c = rl_read_key ();
  338.  
  339.       /* EOF typed to a non-blank line is a <NL>. */
  340.       if (c == EOF && rl_end)
  341.     c = NEWLINE;
  342.  
  343.       /* The character eof_char typed to blank line, and not as the
  344.      previous character is interpreted as EOF. */
  345.       if (((c == eof_char && lastc != c) || c == EOF) && !rl_end)
  346.     {
  347.       eof_found = 1;
  348.       break;
  349.     }
  350.  
  351.       lastc = c;
  352.       rl_dispatch (c, keymap);
  353.  
  354.       /* If there was no change in last_command_was_kill, then no kill
  355.      has taken place.  Note that if input is pending we are reading
  356.      a prefix command, so nothing has changed yet. */
  357.       if (!rl_pending_input)
  358.     {
  359.       if (lk == last_command_was_kill)
  360.         last_command_was_kill = 0;
  361.     }
  362.  
  363. #ifdef VI_MODE
  364.       /* In vi mode, when you exit insert mode, the cursor moves back
  365.      over the previous character.  We explicitly check for that here. */
  366.       if (rl_editing_mode == vi_mode && keymap == vi_movement_keymap)
  367.     rl_vi_check ();
  368. #endif
  369.  
  370.       if (!rl_done)
  371.     rl_redisplay ();
  372.     }
  373.  
  374.   /* Restore the original of this history line, iff the line that we
  375.      are editing was originally in the history, AND the line has changed. */
  376.   {
  377.     HIST_ENTRY *entry = current_history ();
  378.  
  379.     if (entry && rl_undo_list)
  380.       {
  381.     char *temp = savestring (the_line);
  382.     rl_revert_line ();
  383.     entry = replace_history_entry (where_history (), the_line,
  384.                        (HIST_ENTRY *)NULL);
  385.     free_history_entry (entry);
  386.  
  387.     strcpy (the_line, temp);
  388.     free (temp);
  389.       }
  390.   }
  391.  
  392.   /* At any rate, it is highly likely that this line has an undo list.  Get
  393.      rid of it now. */
  394.   if (rl_undo_list)
  395.     free_undo_list ();
  396.  
  397.   if (eof_found)
  398.     return (char *)NULL;
  399.   else
  400.     return (savestring (the_line));
  401. }
  402.  
  403.  
  404. /* **************************************************************** */
  405. /*                                        */
  406. /*               Signal Handling                          */
  407. /*                                    */
  408. /* **************************************************************** */
  409.  
  410. #ifdef SIGWINCH
  411. static SigHandler *old_sigwinch = (SigHandler *)NULL;
  412.  
  413. static sighandler
  414. rl_handle_sigwinch (sig, code, scp)
  415.      int sig, code;
  416.      struct sigcontext *scp;
  417. {
  418.   char *term = rl_terminal_name, *getenv ();
  419.  
  420.   if (readline_echoing_p)
  421.     {
  422.       if (!term)
  423.     term = getenv ("TERM");
  424.       if (!term)
  425.     term = "dumb";
  426.       rl_reset_terminal (term);
  427. #ifdef NEVER
  428.       crlf ();
  429.       rl_forced_update_display ();
  430. #endif
  431.     }
  432.  
  433.   if (old_sigwinch &&
  434.       old_sigwinch != (SigHandler *)SIG_IGN &&
  435.       old_sigwinch != (SigHandler *)SIG_DFL)
  436.     (*old_sigwinch)(sig, code, scp);
  437. }
  438. #endif  /* SIGWINCH */
  439.  
  440. #ifdef HANDLE_SIGNALS
  441. /* Interrupt handling. */
  442. static SigHandler *old_int  = (SigHandler *)NULL,
  443.           *old_tstp = (SigHandler *)NULL,
  444.           *old_ttou = (SigHandler *)NULL,
  445.           *old_ttin = (SigHandler *)NULL,
  446.           *old_cont = (SigHandler *)NULL;
  447.  
  448. /* Handle an interrupt character. */
  449. static sighandler
  450. rl_signal_handler (sig, code, scp)
  451.      int sig, code;
  452.      struct sigcontext *scp;
  453. {
  454.   static rl_prep_terminal (), rl_deprep_terminal ();
  455.  
  456.   switch (sig)
  457.     {
  458.     case SIGINT:
  459.       free_undo_list ();
  460.       rl_clear_message ();
  461.       rl_init_argument ();
  462.  
  463. #ifdef SIGTSTP
  464.     case SIGTSTP:
  465.     case SIGTTOU:
  466.     case SIGTTIN:
  467. #endif
  468.  
  469.       rl_clean_up_for_exit ();
  470.       rl_deprep_terminal ();
  471.       rl_clear_signals ();
  472.       rl_pending_input = 0;
  473.  
  474.       kill (getpid (), sig);
  475.       sigsetmask (0);
  476.  
  477.       rl_prep_terminal ();
  478.       rl_set_signals ();
  479.     }
  480. }
  481.  
  482. rl_set_signals ()
  483. {
  484.   old_int = (SigHandler *)signal (SIGINT, rl_signal_handler);
  485.   if (old_int == (SigHandler *)SIG_IGN)
  486.     signal (SIGINT, SIG_IGN);
  487.  
  488. #ifdef SIGTSTP
  489.   old_tstp = (SigHandler *)signal (SIGTSTP, rl_signal_handler);
  490.   if (old_tstp == (SigHandler *)SIG_IGN)
  491.     signal (SIGTSTP, SIG_IGN);
  492. #endif
  493. #ifdef SIGTTOU
  494.   old_ttou = (SigHandler *)signal (SIGTTOU, rl_signal_handler);
  495.   old_ttin = (SigHandler *)signal (SIGTTIN, rl_signal_handler);
  496.  
  497.   if (old_tstp == (SigHandler *)SIG_IGN)
  498.     {
  499.       signal (SIGTTOU, SIG_IGN);
  500.       signal (SIGTTIN, SIG_IGN);
  501.     }
  502. #endif
  503.  
  504. #ifdef SIGWINCH
  505.   old_sigwinch = (SigHandler *)signal (SIGWINCH, rl_handle_sigwinch);
  506. #endif
  507. }
  508.  
  509. rl_clear_signals ()
  510. {
  511.   signal (SIGINT, old_int);
  512.  
  513. #ifdef SIGTSTP
  514.   signal (SIGTSTP, old_tstp);
  515. #endif
  516.  
  517. #ifdef SIGTTOU
  518.   signal (SIGTTOU, old_ttou);
  519.   signal (SIGTTIN, old_ttin);
  520. #endif
  521.  
  522. #ifdef SIGWINCH
  523.       signal (SIGWINCH, old_sigwinch);
  524. #endif
  525. }
  526. #endif  /* HANDLE_SIGNALS */
  527.  
  528.  
  529. /* **************************************************************** */
  530. /*                                    */
  531. /*            Character Input Buffering               */
  532. /*                                    */
  533. /* **************************************************************** */
  534.  
  535. /* If the terminal was in xoff state when we got to it, then xon_char
  536.    contains the character that is supposed to start it again. */
  537. static int xon_char, xoff_state;
  538. static int pop_index = 0, push_index = 0, ibuffer_len = 511;
  539. static unsigned char ibuffer[512];
  540.  
  541. /* Non-null means it is a pointer to a function to run while waiting for
  542.    character input. */
  543. Function *rl_event_hook = (Function *)NULL;
  544.  
  545. #define any_typein (push_index != pop_index)
  546.  
  547. /* Add KEY to the buffer of characters to be read. */
  548. rl_stuff_char (key)
  549.      int key;
  550. {
  551.   if (key == EOF)
  552.     {
  553.       key = NEWLINE;
  554.       rl_pending_input = EOF;
  555.     }
  556.   ibuffer[push_index++] = key;
  557.   if (push_index >= ibuffer_len)
  558.     push_index = 0;
  559. }
  560.  
  561. /* Return the amount of space available in the
  562.    buffer for stuffing characters. */
  563. int
  564. ibuffer_space ()
  565. {
  566.   if (pop_index > push_index)
  567.     return (pop_index - push_index);
  568.   else
  569.     return (ibuffer_len - (push_index - pop_index));
  570. }
  571.  
  572. /* Get a key from the buffer of characters to be read.
  573.    Return the key in KEY.
  574.    Result is KEY if there was a key, or 0 if there wasn't. */
  575. int
  576. rl_get_char (key)
  577.      int *key;
  578. {
  579.   if (push_index == pop_index)
  580.     return (0);
  581.  
  582.   *key = ibuffer[pop_index++];
  583.  
  584.   if (pop_index >= ibuffer_len)
  585.     pop_index = 0;
  586.  
  587.   return (1);
  588. }
  589.  
  590. /* Stuff KEY into the *front* of the input buffer.
  591.    Returns non-zero if successful, zero if there is
  592.    no space left in the buffer. */
  593. int
  594. rl_unget_char (key)
  595.      int key;
  596. {
  597.   if (ibuffer_space ())
  598.     {
  599.       pop_index--;
  600.       if (pop_index < 0)
  601.     pop_index = ibuffer_len - 1;
  602.       ibuffer[pop_index] = key;
  603.       return (1);
  604.     }
  605.   return (0);
  606. }
  607.  
  608. /* If a character is available to be read, then read it
  609.    and stuff it into IBUFFER.  Otherwise, just return. */
  610. rl_gather_tyi ()
  611. {
  612.   int tty = fileno (in_stream);
  613.   register int tem, result = -1;
  614.   long chars_avail;
  615.   char input;
  616.  
  617. #ifdef FIONREAD
  618.   result = ioctl (tty, FIONREAD, &chars_avail);
  619. #endif
  620.  
  621.   if (result == -1)
  622.     {
  623.       fcntl (tty, F_SETFL, O_NDELAY);
  624.       chars_avail = read (tty, &input, 1);
  625.       fcntl (tty, F_SETFL, 0);
  626.       if (chars_avail == -1 && errno == EAGAIN)
  627.     return;
  628.     }
  629.  
  630.   tem = ibuffer_space ();
  631.  
  632.   if (chars_avail > tem)
  633.     chars_avail = tem;
  634.  
  635.   /* One cannot read all of the available input.  I can only read a single
  636.      character at a time, or else programs which require input can be
  637.      thwarted.  If the buffer is larger than one character, I lose.
  638.      Damn! */
  639.   if (tem < ibuffer_len)
  640.     chars_avail = 0;
  641.  
  642.   if (result != -1)
  643.     {
  644.       while (chars_avail--)
  645.     rl_stuff_char (rl_getc (in_stream));
  646.     }
  647.   else
  648.     {
  649.       if (chars_avail)
  650.     rl_stuff_char (input);
  651.     }
  652. }
  653.  
  654. /* Read a key, including pending input. */
  655. int
  656. rl_read_key ()
  657. {
  658.   int c;
  659.  
  660.   rl_key_sequence_length++;
  661.  
  662.   if (rl_pending_input)
  663.     {
  664.       c = rl_pending_input;
  665.       rl_pending_input = 0;
  666.     }
  667.   else
  668.     {
  669.       static int next_macro_key ();
  670.  
  671.       /* If input is coming from a macro, then use that. */
  672.       if (c = next_macro_key ())
  673.     return (c);
  674.  
  675.       /* If the user has an event function, then call it periodically. */
  676.       if (rl_event_hook)
  677.     {
  678.       while (rl_event_hook && !rl_get_char (&c))
  679.         {
  680.           (*rl_event_hook) ();
  681.           rl_gather_tyi ();
  682.         }
  683.     }
  684.       else
  685.     {
  686.       if (!rl_get_char (&c))
  687.         c = rl_getc (in_stream);
  688.     }
  689.     }
  690.  
  691. #ifdef NEVER  /* This breaks supdup to 4.0.3c machines. */
  692. #ifdef TIOCSTART
  693.   /* Ugh.  But I can't think of a better way. */
  694.   if (xoff_state && c == xon_char)
  695.     {
  696.       ioctl (fileno (in_stream), TIOCSTART, 0);
  697.       xoff_state = 0;
  698.       return (rl_read_key ());
  699.     }
  700. #endif /* TIOCSTART */
  701. #endif
  702.  
  703.   return (c);
  704. }
  705.  
  706. /* I'm beginning to hate the declaration rules for various compilers. */
  707. static void add_macro_char ();
  708.  
  709. /* Do the command associated with KEY in MAP.
  710.    If the associated command is really a keymap, then read
  711.    another key, and dispatch into that map. */
  712. rl_dispatch (key, map)
  713.      register int key;
  714.      Keymap map;
  715. {
  716.  
  717.   if (defining_kbd_macro)
  718.     add_macro_char (key);
  719.  
  720.   if (key > 127 && key < 256)
  721.     {
  722.       if (map[ESC].type == ISKMAP)
  723.     {
  724.       map = (Keymap)map[ESC].function;
  725.       key -= 128;
  726.       rl_dispatch (key, map);
  727.     }
  728.       else
  729.     ding ();
  730.       return;
  731.     }
  732.  
  733.   switch (map[key].type)
  734.     {
  735.     case ISFUNC:
  736.       {
  737.     Function *func = map[key].function;
  738.  
  739.     if (func != (Function *)NULL)
  740.       {
  741.         /* Special case rl_do_lowercase_version (). */
  742.         if (func == rl_do_lowercase_version)
  743.           {
  744.         rl_dispatch (to_lower (key), map);
  745.         return;
  746.           }
  747.  
  748.         (*map[key].function)(rl_numeric_arg * arg_sign, key);
  749.       }
  750.     else
  751.       {
  752.         ding ();
  753.         return;
  754.       }
  755.       }
  756.       break;
  757.  
  758.     case ISKMAP:
  759.       if (map[key].function != (Function *)NULL)
  760.     {
  761.       int newkey;
  762.  
  763.       rl_key_sequence_length++;
  764.       newkey = rl_read_key ();
  765.       rl_dispatch (newkey, (Keymap)map[key].function);
  766.     }
  767.       else
  768.     {
  769.       ding ();
  770.       return;
  771.     }
  772.       break;
  773.  
  774.     case ISMACR:
  775.       if (map[key].function != (Function *)NULL)
  776.     {
  777.       static with_macro_input ();
  778.       char *macro = savestring ((char *)map[key].function);
  779.  
  780.       with_macro_input (macro);
  781.       return;
  782.     }
  783.       break;
  784.     }
  785.  
  786.   /* If we have input pending, then the last command was a prefix
  787.      command.  Don't change the state of rl_last_func. */
  788.   if (!rl_pending_input)
  789.     rl_last_func = map[key].function;
  790. }
  791.  
  792.  
  793. /* **************************************************************** */
  794. /*                                    */
  795. /*            Hacking Keyboard Macros             */
  796. /*                                    */
  797. /* **************************************************************** */
  798.  
  799. /* The currently executing macro string.  If this is non-zero,
  800.    then it is a malloc ()'ed string where input is coming from. */
  801. static char *executing_macro = (char *)NULL;
  802.  
  803. /* The offset in the above string to the next character to be read. */
  804. static int executing_macro_index = 0;
  805.  
  806. /* The current macro string being built.  Characters get stuffed
  807.    in here by add_macro_char (). */
  808. static char *current_macro = (char *)NULL;
  809.  
  810. /* The size of the buffer allocated to current_macro. */
  811. static int current_macro_size = 0;
  812.  
  813. /* The index at which characters are being added to current_macro. */
  814. static int current_macro_index = 0;
  815.  
  816. /* A structure used to save nested macro strings.
  817.    It is a linked list of string/index for each saved macro. */
  818. struct saved_macro {
  819.   struct saved_macro *next;
  820.   char *string;
  821.   int index;
  822. };
  823.  
  824. /* The list of saved macros. */
  825. struct saved_macro *macro_list = (struct saved_macro *)NULL;
  826.  
  827. /* Forward declarations of static functions.  Thank you C. */
  828. static void push_executing_macro (), pop_executing_macro ();
  829.  
  830. /* This one has to be declared earlier in the file. */
  831. /* static void add_macro_char (); */
  832.  
  833. /* Set up to read subsequent input from STRING.
  834.    STRING is free ()'ed when we are done with it. */
  835. static
  836. with_macro_input (string)
  837.      char *string;
  838. {
  839.   push_executing_macro ();
  840.   executing_macro = string;
  841.   executing_macro_index = 0;
  842. }
  843.  
  844. /* Return the next character available from a macro, or 0 if
  845.    there are no macro characters. */
  846. static int
  847. next_macro_key ()
  848. {
  849.   if (!executing_macro)
  850.     return (0);
  851.  
  852.   if (!executing_macro[executing_macro_index])
  853.     {
  854.       pop_executing_macro ();
  855.       return (next_macro_key ());
  856.     }
  857.  
  858.   return (executing_macro[executing_macro_index++]);
  859. }
  860.  
  861. /* Save the currently executing macro on a stack of saved macros. */
  862. static void
  863. push_executing_macro ()
  864. {
  865.   struct saved_macro *saver;
  866.  
  867.   saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro));
  868.   saver->next = macro_list;
  869.   saver->index = executing_macro_index;
  870.   saver->string = executing_macro;
  871.  
  872.   macro_list = saver;
  873. }
  874.  
  875. /* Discard the current macro, replacing it with the one
  876.    on the top of the stack of saved macros. */
  877. static void
  878. pop_executing_macro ()
  879. {
  880.   if (executing_macro)
  881.     free (executing_macro);
  882.  
  883.   executing_macro = (char *)NULL;
  884.   executing_macro_index = 0;
  885.  
  886.   if (macro_list)
  887.     {
  888.       struct saved_macro *disposer = macro_list;
  889.       executing_macro = macro_list->string;
  890.       executing_macro_index = macro_list->index;
  891.       macro_list = macro_list->next;
  892.       free (disposer);
  893.     }
  894. }
  895.  
  896. /* Add a character to the macro being built. */
  897. static void
  898. add_macro_char (c)
  899.      int c;
  900. {
  901.   if (current_macro_index + 1 >= current_macro_size)
  902.     {
  903.       if (!current_macro)
  904.     current_macro = (char *)xmalloc (current_macro_size = 25);
  905.       else
  906.     current_macro =
  907.       (char *)xrealloc (current_macro, current_macro_size += 25);
  908.     }
  909.  
  910.   current_macro[current_macro_index++] = c;
  911.   current_macro[current_macro_index] = '\0';
  912. }
  913.  
  914. /* Begin defining a keyboard macro.
  915.    Keystrokes are recorded as they are executed.
  916.    End the definition with rl_end_kbd_macro ().
  917.    If a numeric argument was explicitly typed, then append this
  918.    definition to the end of the existing macro, and start by
  919.    re-executing the existing macro. */
  920. rl_start_kbd_macro (ignore1, ignore2)
  921.      int ignore1, ignore2;
  922. {
  923.   if (defining_kbd_macro)
  924.     rl_abort ();
  925.  
  926.   if (rl_explicit_arg)
  927.     {
  928.       if (current_macro)
  929.     with_macro_input (savestring (current_macro));
  930.     }
  931.   else
  932.     current_macro_index = 0;
  933.  
  934.   defining_kbd_macro = 1;
  935. }
  936.  
  937. /* Stop defining a keyboard macro.
  938.    A numeric argument says to execute the macro right now,
  939.    that many times, counting the definition as the first time. */
  940. rl_end_kbd_macro (count, ignore)
  941.      int count, ignore;
  942. {
  943.   if (!defining_kbd_macro)
  944.     rl_abort ();
  945.  
  946.   current_macro_index -= (rl_key_sequence_length - 1);
  947.   current_macro[current_macro_index] = '\0';
  948.  
  949.   defining_kbd_macro = 0;
  950.  
  951.   rl_call_last_kbd_macro (--count, 0);
  952. }
  953.  
  954. /* Execute the most recently defined keyboard macro.
  955.    COUNT says how many times to execute it. */
  956. rl_call_last_kbd_macro (count, ignore)
  957.      int count, ignore;
  958. {
  959.   if (!current_macro)
  960.     rl_abort ();
  961.  
  962.   while (count--)
  963.     with_macro_input (savestring (current_macro));
  964. }
  965.  
  966.  
  967. /* **************************************************************** */
  968. /*                                    */
  969. /*            Initializations                 */
  970. /*                                    */
  971. /* **************************************************************** */
  972.  
  973. /* Initliaze readline (and terminal if not already). */
  974. rl_initialize ()
  975. {
  976.   extern char *rl_display_prompt;
  977.  
  978.   /* If we have never been called before, initialize the
  979.      terminal and data structures. */
  980.   if (!rl_initialized)
  981.     {
  982.       readline_initialize_everything ();
  983.       rl_initialized++;
  984.     }
  985.  
  986.   /* Initalize the current line information. */
  987.   rl_point = rl_end = 0;
  988.   the_line = rl_line_buffer;
  989.   the_line[0] = 0;
  990.  
  991.   /* We aren't done yet.  We haven't even gotten started yet! */
  992.   rl_done = 0;
  993.  
  994.   /* Tell the history routines what is going on. */
  995.   start_using_history ();
  996.  
  997.   /* Make the display buffer match the state of the line. */
  998.   {
  999.     extern char *rl_display_prompt;
  1000.     extern int forced_display;
  1001.  
  1002.     rl_on_new_line ();
  1003.  
  1004.     rl_display_prompt = rl_prompt ? rl_prompt : "";
  1005.     forced_display = 1;
  1006.   }
  1007.  
  1008.   /* No such function typed yet. */
  1009.   rl_last_func = (Function *)NULL;
  1010.  
  1011.   /* Parsing of key-bindings begins in an enabled state. */
  1012.   parsing_conditionalized_out = 0;
  1013. }
  1014.  
  1015. /* Initialize the entire state of the world. */
  1016. readline_initialize_everything ()
  1017. {
  1018.   /* Find out if we are running in Emacs. */
  1019.   running_in_emacs = (char *)getenv ("EMACS");
  1020.  
  1021.   /* Allocate data structures. */
  1022.   if (!rl_line_buffer)
  1023.     rl_line_buffer =
  1024.       (char *)xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
  1025.  
  1026.   /* Initialize the terminal interface. */
  1027.   init_terminal_io ((char *)NULL);
  1028.  
  1029.   /* Bind tty characters to readline functions. */
  1030.   readline_default_bindings ();
  1031.  
  1032.   /* Initialize the function names. */
  1033.   rl_initialize_funmap ();
  1034.  
  1035.   /* Read in the init file. */
  1036.   rl_read_init_file ((char *)NULL);
  1037.  
  1038.   /* If the completion parser's default word break characters haven't
  1039.      been set yet, then do so now. */
  1040.   {
  1041.     extern char *rl_completer_word_break_characters;
  1042.     extern char *rl_basic_word_break_characters;
  1043.  
  1044.     if (rl_completer_word_break_characters == (char *)NULL)
  1045.       rl_completer_word_break_characters = rl_basic_word_break_characters;
  1046.   }
  1047. }
  1048.  
  1049. /* If this system allows us to look at the values of the regular
  1050.    input editing characters, then bind them to their readline
  1051.    equivalents. */
  1052. readline_default_bindings ()
  1053. {
  1054.  
  1055. #ifdef NEW_TTY_DRIVER
  1056.   struct sgttyb ttybuff;
  1057.   int tty = fileno (rl_instream);
  1058.  
  1059.   if (ioctl (tty, TIOCGETP, &ttybuff) != -1)
  1060.     {
  1061.       int erase = ttybuff.sg_erase, kill = ttybuff.sg_kill;
  1062.  
  1063.       if (erase != -1 && keymap[erase].type == ISFUNC)
  1064.     keymap[erase].function = rl_rubout;
  1065.  
  1066.       if (kill != -1 && keymap[kill].type == ISFUNC)
  1067.     keymap[kill].function = rl_unix_line_discard;
  1068.     }
  1069.  
  1070. #ifdef TIOCGLTC
  1071.   {
  1072.     struct ltchars lt;
  1073.  
  1074.     if (ioctl (tty, TIOCGLTC, <) != -1)
  1075.       {
  1076.     int erase = lt.t_werasc, nextc = lt.t_lnextc;
  1077.  
  1078.     if (erase != -1 && keymap[erase].type == ISFUNC)
  1079.       keymap[erase].function = rl_unix_word_rubout;
  1080.  
  1081.     if (nextc != -1 && keymap[nextc].type == ISFUNC)
  1082.       keymap[nextc].function = rl_quoted_insert;
  1083.       }
  1084.   }
  1085. #endif /* TIOCGLTC */
  1086. #else /* not NEW_TTY_DRIVER */
  1087.   struct termio ttybuff;
  1088.   int tty = fileno (rl_instream);
  1089.  
  1090.   if (ioctl (tty, TCGETA, &ttybuff) != -1)
  1091.     {
  1092.       int erase = ttybuff.c_cc[VERASE];
  1093.       int kill = ttybuff.c_cc[VKILL];
  1094.  
  1095.       if (erase != -1 && keymap[(unsigned char)erase].type == ISFUNC)
  1096.     keymap[(unsigned char)erase].function = rl_rubout;
  1097.  
  1098.       if (kill != -1 && keymap[(unsigned char)kill].type == ISFUNC)
  1099.     keymap[(unsigned char)kill].function = rl_unix_line_discard;
  1100.     }
  1101. #endif /* NEW_TTY_DRIVER */
  1102. }
  1103.  
  1104.  
  1105. /* **************************************************************** */
  1106. /*                                    */
  1107. /*            Numeric Arguments                */
  1108. /*                                    */
  1109. /* **************************************************************** */
  1110.  
  1111. /* Handle C-u style numeric args, as well as M--, and M-digits. */
  1112.  
  1113. /* Add the current digit to the argument in progress. */
  1114. rl_digit_argument (ignore, key)
  1115.      int ignore, key;
  1116. {
  1117.   rl_pending_input = key;
  1118.   rl_digit_loop ();
  1119. }
  1120.  
  1121. /* What to do when you abort reading an argument. */
  1122. rl_discard_argument ()
  1123. {
  1124.   ding ();
  1125.   rl_clear_message ();
  1126.   rl_init_argument ();
  1127. }
  1128.  
  1129. /* Create a default argument. */
  1130. rl_init_argument ()
  1131. {
  1132.   rl_numeric_arg = arg_sign = 1;
  1133.   rl_explicit_arg = 0;
  1134. }
  1135.  
  1136. /* C-u, universal argument.  Multiply the current argument by 4.
  1137.    Read a key.  If the key has nothing to do with arguments, then
  1138.    dispatch on it.  If the key is the abort character then abort. */
  1139. rl_universal_argument ()
  1140. {
  1141.   rl_numeric_arg *= 4;
  1142.   rl_digit_loop ();
  1143. }
  1144.  
  1145. rl_digit_loop ()
  1146. {
  1147.   int key, c;
  1148.   while (1)
  1149.     {
  1150.       rl_message ("(arg: %d) ", arg_sign * rl_numeric_arg);
  1151.       key = c = rl_read_key ();
  1152.  
  1153.       if (keymap[c].type == ISFUNC &&
  1154.       keymap[c].function == rl_universal_argument)
  1155.     {
  1156.       rl_numeric_arg *= 4;
  1157.       continue;
  1158.     }
  1159.       c = UNMETA (c);
  1160.       if (numeric (c))
  1161.     {
  1162.       if (rl_explicit_arg)
  1163.         rl_numeric_arg = (rl_numeric_arg * 10) + (c - '0');
  1164.       else
  1165.         rl_numeric_arg = (c - '0');
  1166.       rl_explicit_arg = 1;
  1167.     }
  1168.       else
  1169.     {
  1170.       if (c == '-' && !rl_explicit_arg)
  1171.         {
  1172.           rl_numeric_arg = 1;
  1173.           arg_sign = -1;
  1174.         }
  1175.       else
  1176.         {
  1177.           rl_clear_message ();
  1178.           rl_dispatch (key, keymap);
  1179.           return;
  1180.         }
  1181.     }
  1182.     }
  1183. }
  1184.  
  1185.  
  1186. /* **************************************************************** */
  1187. /*                                    */
  1188. /*            Display stuff                    */
  1189. /*                                    */
  1190. /* **************************************************************** */
  1191.  
  1192. /* This is the stuff that is hard for me.  I never seem to write good
  1193.    display routines in C.  Let's see how I do this time. */
  1194.  
  1195. /* (PWP) Well... Good for a simple line updater, but totally ignores
  1196.    the problems of input lines longer than the screen width.
  1197.  
  1198.    update_line and the code that calls it makes a multiple line,
  1199.    automatically wrapping line update.  Carefull attention needs
  1200.    to be paid to the vertical position variables.
  1201.  
  1202.    handling of terminals with autowrap on (incl. DEC braindamage)
  1203.    could be improved a bit.  Right now I just cheat and decrement
  1204.    screenwidth by one. */
  1205.  
  1206. /* Keep two buffers; one which reflects the current contents of the
  1207.    screen, and the other to draw what we think the new contents should
  1208.    be.  Then compare the buffers, and make whatever changes to the
  1209.    screen itself that we should.  Finally, make the buffer that we
  1210.    just drew into be the one which reflects the current contents of the
  1211.    screen, and place the cursor where it belongs.
  1212.  
  1213.    Commands that want to can fix the display themselves, and then let
  1214.    this function know that the display has been fixed by setting the
  1215.    RL_DISPLAY_FIXED variable.  This is good for efficiency. */
  1216.  
  1217. /* Termcap variables: */
  1218. extern char *term_up, *term_dc, *term_cr;
  1219. extern int screenheight, screenwidth, terminal_can_insert;
  1220.  
  1221. /* What YOU turn on when you have handled all redisplay yourself. */
  1222. int rl_display_fixed = 0;
  1223.  
  1224. /* The visible cursor position.  If you print some text, adjust this. */
  1225. int last_c_pos = 0;
  1226. int last_v_pos = 0;
  1227.  
  1228. /* The last left edge of text that was displayed.  This is used when
  1229.    doing horizontal scrolling.  It shifts in thirds of a screenwidth. */
  1230. static int last_lmargin = 0;
  1231.  
  1232. /* The line display buffers.  One is the line currently displayed on
  1233.    the screen.  The other is the line about to be displayed. */
  1234. static char *visible_line = (char *)NULL;
  1235. static char *invisible_line = (char *)NULL;
  1236.  
  1237. /* Number of lines currently on screen minus 1. */
  1238. int vis_botlin = 0;
  1239.  
  1240. /* A buffer for `modeline' messages. */
  1241. char msg_buf[128];
  1242.  
  1243. /* Non-zero forces the redisplay even if we thought it was unnecessary. */
  1244. int forced_display = 0;
  1245.  
  1246. /* The stuff that gets printed out before the actual text of the line.
  1247.    This is usually pointing to rl_prompt. */
  1248. char *rl_display_prompt = (char *)NULL;
  1249.  
  1250. /* Default and initial buffer size.  Can grow. */
  1251. static int line_size = 1024;
  1252.  
  1253. /* Non-zero means to always use horizontal scrolling in line display. */
  1254. static int horizontal_scroll_mode = 0;
  1255.  
  1256. /* Non-zero means to display an asterisk at the starts of history lines
  1257.    which have been modified. */
  1258. static int mark_modified_lines = 0;
  1259.  
  1260. /* I really disagree with this, but my boss (among others) insists that we
  1261.    support compilers that don't work.  I don't think we are gaining by doing
  1262.    so; what is the advantage in producing better code if we can't use it? */
  1263. /* The following two declarations belong inside the
  1264.    function block, not here. */
  1265. static void move_cursor_relative ();
  1266. static void output_some_chars ();
  1267. static void output_character_function ();
  1268. static int compare_strings ();
  1269.  
  1270. /* Basic redisplay algorithm. */
  1271. rl_redisplay ()
  1272. {
  1273.   register int in, out, c, linenum;
  1274.   register char *line = invisible_line;
  1275.   int c_pos = 0;
  1276.   int inv_botlin = 0;        /* Number of lines in newly drawn buffer. */
  1277.  
  1278.   extern int readline_echoing_p;
  1279.  
  1280.   if (!readline_echoing_p)
  1281.     return;
  1282.  
  1283.   if (!rl_display_prompt)
  1284.     rl_display_prompt = "";
  1285.  
  1286.   if (!invisible_line)
  1287.     {
  1288.       visible_line = (char *)xmalloc (line_size);
  1289.       invisible_line = (char *)xmalloc (line_size);
  1290.       line = invisible_line;
  1291.       for (in = 0; in < line_size; in++)
  1292.     {
  1293.       visible_line[in] = 0;
  1294.       invisible_line[in] = 1;
  1295.     }
  1296.       rl_on_new_line ();
  1297.     }
  1298.  
  1299.   /* Draw the line into the buffer. */
  1300.   c_pos = -1;
  1301.  
  1302.   /* Mark the line as modified or not.  We only do this for history
  1303.      lines. */
  1304.   out = 0;
  1305.   if (mark_modified_lines && current_history () && rl_undo_list)
  1306.     {
  1307.       line[out++] = '*';
  1308.       line[out] = '\0';
  1309.     }
  1310.  
  1311.   /* If someone thought that the redisplay was handled, but the currently
  1312.      visible line has a different modification state than the one about
  1313.      to become visible, then correct the callers misconception. */
  1314.   if (visible_line[0] != invisible_line[0])
  1315.     rl_display_fixed = 0;
  1316.  
  1317.   strncpy (line + out,  rl_display_prompt, strlen (rl_display_prompt));
  1318.   out += strlen (rl_display_prompt);
  1319.   line[out] = '\0';
  1320.  
  1321.   for (in = 0; in < rl_end; in++)
  1322.     {
  1323.       c = the_line[in];
  1324.  
  1325.       if (out + 1 >= line_size)
  1326.     {
  1327.       line_size *= 2;
  1328.       visible_line = (char *)xrealloc (visible_line, line_size);
  1329.       invisible_line = (char *)xrealloc (invisible_line, line_size);
  1330.       line = invisible_line;
  1331.     }
  1332.  
  1333.       if (in == rl_point)
  1334.     c_pos = out;
  1335.  
  1336.       if (c > 127)
  1337.     {
  1338.       line[out++] = 'M';
  1339.       line[out++] = '-';
  1340.       line[out++] = c - 128;
  1341.     }
  1342. #define DISPLAY_TABS
  1343. #ifdef DISPLAY_TABS
  1344.       else if (c == '\t')
  1345.     {
  1346.       register int newout = (out | (int)7) + 1;
  1347.       while (out < newout)
  1348.         line[out++] = ' ';
  1349.     }
  1350. #endif
  1351.       else if (c < 32)
  1352.     {
  1353.       line[out++] = 'C';
  1354.       line[out++] = '-';
  1355.       line[out++] = c + 64;
  1356.     }
  1357.       else
  1358.     line[out++] = c;
  1359.     }
  1360.   line[out] = '\0';
  1361.   if (c_pos < 0)
  1362.     c_pos = out;
  1363.  
  1364.   /* PWP: now is when things get a bit hairy.  The visible and invisible
  1365.      line buffers are really multiple lines, which would wrap every
  1366.      (screenwidth - 1) characters.  Go through each in turn, finding
  1367.      the changed region and updating it.  The line order is top to bottom. */
  1368.  
  1369.   /* If we can move the cursor up and down, then use multiple lines,
  1370.      otherwise, let long lines display in a single terminal line, and
  1371.      horizontally scroll it. */
  1372.  
  1373.   if (!horizontal_scroll_mode && term_up && *term_up)
  1374.     {
  1375.       int total_screen_chars = (screenwidth * screenheight);
  1376.  
  1377.       if (!rl_display_fixed || forced_display)
  1378.     {
  1379.       forced_display = 0;
  1380.  
  1381.       /* If we have more than a screenful of material to display, then
  1382.          only display a screenful.  We should display the last screen,
  1383.          not the first.  I'll fix this in a minute. */
  1384.       if (out >= total_screen_chars)
  1385.         out = total_screen_chars - 1;
  1386.  
  1387.       /* Number of screen lines to display. */
  1388.       inv_botlin = out / screenwidth;
  1389.  
  1390.       /* For each line in the buffer, do the updating display. */
  1391.       for (linenum = 0; linenum <= inv_botlin; linenum++)
  1392.         update_line (linenum > vis_botlin ? ""
  1393.              : &visible_line[linenum * screenwidth],
  1394.              &invisible_line[linenum * screenwidth],
  1395.              linenum);
  1396.  
  1397.       /* We may have deleted some lines.  If so, clear the left over
  1398.          blank ones at the bottom out. */
  1399.       if (vis_botlin > inv_botlin)
  1400.         {
  1401.           char *tt;
  1402.           for (; linenum <= vis_botlin; linenum++)
  1403.         {
  1404.           tt = &visible_line[linenum * screenwidth];
  1405.           move_vert (linenum);
  1406.           move_cursor_relative (0, tt);
  1407.           clear_to_eol ((linenum == vis_botlin)?
  1408.                 strlen (tt) : screenwidth);
  1409.         }
  1410.         }
  1411.       vis_botlin = inv_botlin;
  1412.  
  1413.       /* Move the cursor where it should be. */
  1414.       move_vert (c_pos / screenwidth);
  1415.       move_cursor_relative (c_pos % screenwidth,
  1416.                 &invisible_line[(c_pos / screenwidth) * screenwidth]);
  1417.     }
  1418.     }
  1419.   else                /* Do horizontal scrolling. */
  1420.     {
  1421.       int lmargin;
  1422.  
  1423.       /* Always at top line. */
  1424.       last_v_pos = 0;
  1425.  
  1426.       /* If the display position of the cursor would be off the edge
  1427.      of the screen, start the display of this line at an offset that
  1428.      leaves the cursor on the screen. */
  1429.       if (c_pos - last_lmargin > screenwidth - 2)
  1430.     lmargin = (c_pos / (screenwidth / 3) - 2) * (screenwidth / 3);
  1431.       else if (c_pos - last_lmargin < 1)
  1432.     lmargin = ((c_pos - 1) / (screenwidth / 3)) * (screenwidth / 3);
  1433.       else
  1434.     lmargin = last_lmargin;
  1435.  
  1436.       /* If the first character on the screen isn't the first character
  1437.      in the display line, indicate this with a special character. */
  1438.       if (lmargin > 0)
  1439.     line[lmargin] = '<';
  1440.  
  1441.       if (lmargin + screenwidth < out)
  1442.     line[lmargin + screenwidth - 1] = '>';
  1443.  
  1444.       if (!rl_display_fixed || forced_display || lmargin != last_lmargin)
  1445.     {
  1446.       forced_display = 0;
  1447.       update_line (&visible_line[last_lmargin],
  1448.                &invisible_line[lmargin], 0);
  1449.  
  1450.       move_cursor_relative (c_pos - lmargin, &invisible_line[lmargin]);
  1451.       last_lmargin = lmargin;
  1452.     }
  1453.     }
  1454.   fflush (out_stream);
  1455.  
  1456.   /* Swap visible and non-visible lines. */
  1457.   {
  1458.     char *temp = visible_line;
  1459.     visible_line = invisible_line;
  1460.     invisible_line = temp;
  1461.     rl_display_fixed = 0;
  1462.   }
  1463. }
  1464.  
  1465. /* PWP: update_line() is based on finding the middle difference of each
  1466.    line on the screen; vis:
  1467.  
  1468.                  /old first difference
  1469.     /beginning of line   |              /old last same       /old EOL
  1470.     v             v              v                    v
  1471. old:    eddie> Oh, my little gruntle-buggy is to me, as lurgid as
  1472. new:    eddie> Oh, my little buggy says to me, as lurgid as
  1473.     ^             ^        ^               ^
  1474.     \beginning of line   |        \new last same       \new end of line
  1475.                  \new first difference
  1476.  
  1477.    All are character pointers for the sake of speed.  Special cases for
  1478.    no differences, as well as for end of line additions must be handeled.
  1479.  
  1480.    Could be made even smarter, but this works well enough */
  1481. static
  1482. update_line (old, new, current_line)
  1483.      register char *old, *new;
  1484.      int current_line;
  1485. {
  1486.   register char *ofd, *ols, *oe, *nfd, *nls, *ne;
  1487.   int lendiff, wsatend;
  1488.  
  1489.   /* Find first difference. */
  1490.   for (ofd = old, nfd = new;
  1491.        (ofd - old < screenwidth) && *ofd && (*ofd == *nfd);
  1492.        ofd++, nfd++)
  1493.     ;
  1494.  
  1495.   /* Move to the end of the screen line. */
  1496.   for (oe = ofd; ((oe - old) < screenwidth) && *oe; oe++);
  1497.   for (ne = nfd; ((ne - new) < screenwidth) && *ne; ne++);
  1498.  
  1499.   /* If no difference, continue to next line. */
  1500.   if (ofd == oe && nfd == ne)
  1501.     return;
  1502.  
  1503.   wsatend = 1;            /* flag for trailing whitespace */
  1504.   ols = oe - 1;            /* find last same */
  1505.   nls = ne - 1;
  1506.   while ((ols > ofd) && (nls > nfd) && (*ols == *nls))
  1507.     {
  1508.       if (*ols != ' ')
  1509.     wsatend = 0;
  1510.       ols--;
  1511.       nls--;
  1512.     }
  1513.  
  1514.   if (wsatend)
  1515.     {
  1516.       ols = oe;
  1517.       nls = ne;
  1518.     }
  1519.   else if (*ols != *nls)
  1520.     {
  1521.       if (*ols)            /* don't step past the NUL */
  1522.     ols++;
  1523.       if (*nls)
  1524.     nls++;
  1525.     }
  1526.  
  1527.   move_vert (current_line);
  1528.   move_cursor_relative (ofd - old, old);
  1529.  
  1530.   /* if (len (new) > len (old)) */
  1531.   lendiff = (nls - nfd) - (ols - ofd);
  1532.  
  1533.   /* Insert (diff(len(old),len(new)) ch */
  1534.   if (lendiff > 0)
  1535.     {
  1536.       if (terminal_can_insert)
  1537.     {
  1538.       extern char *term_IC;
  1539.  
  1540.       /* Sometimes it is cheaper to print the characters rather than
  1541.          use the terminal's capabilities. */
  1542.       if ((2 * (ne - nfd)) < lendiff && !term_IC)
  1543.         {
  1544.           output_some_chars (nfd, (ne - nfd));
  1545.           last_c_pos += (ne - nfd);
  1546.         }
  1547.       else
  1548.         {
  1549.           if (*ols)
  1550.         {
  1551.           insert_some_chars (nfd, lendiff);
  1552.           last_c_pos += lendiff;
  1553.         }
  1554.           else
  1555.         {
  1556.           /* At the end of a line the characters do not have to
  1557.              be "inserted".  They can just be placed on the screen. */
  1558.           output_some_chars (nfd, lendiff);
  1559.           last_c_pos += lendiff;
  1560.         }
  1561.           /* Copy (new) chars to screen from first diff to last match. */
  1562.           if (((nls - nfd) - lendiff) > 0)
  1563.         {
  1564.           output_some_chars (&nfd[lendiff], ((nls - nfd) - lendiff));
  1565.           last_c_pos += ((nls - nfd) - lendiff);
  1566.         }
  1567.         }
  1568.     }
  1569.       else
  1570.     {        /* cannot insert chars, write to EOL */
  1571.       output_some_chars (nfd, (ne - nfd));
  1572.       last_c_pos += (ne - nfd);
  1573.     }
  1574.     }
  1575.   else                /* Delete characters from line. */
  1576.     {
  1577.       /* If possible and inexpensive to use terminal deletion, then do so. */
  1578.       if (term_dc && (2 * (ne - nfd)) >= (-lendiff))
  1579.     {
  1580.       if (lendiff)
  1581.         delete_chars (-lendiff); /* delete (diff) characters */
  1582.  
  1583.       /* Copy (new) chars to screen from first diff to last match */
  1584.       if ((nls - nfd) > 0)
  1585.         {
  1586.           output_some_chars (nfd, (nls - nfd));
  1587.           last_c_pos += (nls - nfd);
  1588.         }
  1589.     }
  1590.       /* Otherwise, print over the existing material. */
  1591.       else
  1592.     {
  1593.       output_some_chars (nfd, (ne - nfd));
  1594.       last_c_pos += (ne - nfd);
  1595.       clear_to_eol ((oe - old) - (ne - new));
  1596.     }
  1597.     }
  1598. }
  1599.  
  1600. /* (PWP) tell the update routines that we have moved onto a
  1601.    new (empty) line. */
  1602. rl_on_new_line ()
  1603. {
  1604.   if (visible_line)
  1605.     visible_line[0] = '\0';
  1606.  
  1607.   last_c_pos = last_v_pos = 0;
  1608.   vis_botlin = last_lmargin = 0;
  1609. }
  1610.  
  1611. /* Actually update the display, period. */
  1612. rl_forced_update_display ()
  1613. {
  1614.   if (visible_line)
  1615.     {
  1616.       register char *temp = visible_line;
  1617.  
  1618.       while (*temp) *temp++ = '\0';
  1619.     }
  1620.   rl_on_new_line ();
  1621.   forced_display++;
  1622.   rl_redisplay ();
  1623. }
  1624.  
  1625. /* Move the cursor from last_c_pos to NEW, which are buffer indices.
  1626.    DATA is the contents of the screen line of interest; i.e., where
  1627.    the movement is being done. */
  1628. static void
  1629. move_cursor_relative (new, data)
  1630.      int new;
  1631.      char *data;
  1632. {
  1633.   register int i;
  1634.  
  1635.   /* It may be faster to output a CR, and then move forwards instead
  1636.      of moving backwards. */
  1637.   if (new + 1 < last_c_pos - new)
  1638.     {
  1639.       tputs (term_cr, 1, output_character_function);
  1640.       last_c_pos = 0;
  1641.     }
  1642.  
  1643.   if (last_c_pos == new) return;
  1644.  
  1645.   if (last_c_pos < new)
  1646.     {
  1647.       /* Move the cursor forward.  We do it by printing the command
  1648.      to move the cursor forward if there is one, else print that
  1649.      portion of the output buffer again.  Which is cheaper? */
  1650.  
  1651.       /* The above comment is left here for posterity.  It is faster
  1652.      to print one character (non-control) than to print a control
  1653.      sequence telling the terminal to move forward one character.
  1654.      That kind of control is for people who don't know what the
  1655.      data is underneath the cursor. */
  1656. #ifdef HACK_TERMCAP_MOTION
  1657.       extern char *term_forward_char;
  1658.  
  1659.       if (term_forward_char)
  1660.     for (i = last_c_pos; i < new; i++)
  1661.       tputs (term_forward_char, 1, output_character_function);
  1662.       else
  1663.     for (i = last_c_pos; i < new; i++)
  1664.       putc (data[i], out_stream);
  1665. #else
  1666.       for (i = last_c_pos; i < new; i++)
  1667.     putc (data[i], out_stream);
  1668. #endif                /* HACK_TERMCAP_MOTION */
  1669.     }
  1670.   else
  1671.     backspace (last_c_pos - new);
  1672.   last_c_pos = new;
  1673. }
  1674.  
  1675. /* PWP: move the cursor up or down. */
  1676. move_vert (to)
  1677.      int to;
  1678. {
  1679.   void output_character_function ();
  1680.   register int delta, i;
  1681.  
  1682.   if (last_v_pos == to) return;
  1683.  
  1684.   if (to > screenheight)
  1685.     return;
  1686.  
  1687.   if ((delta = to - last_v_pos) > 0)
  1688.     {
  1689.       for (i = 0; i < delta; i++)
  1690.     putc ('\n', out_stream);
  1691.       tputs (term_cr, 1, output_character_function);
  1692.       last_c_pos = 0;        /* because crlf() will do \r\n */
  1693.     }
  1694.   else
  1695.     {            /* delta < 0 */
  1696.       if (term_up && *term_up)
  1697.     for (i = 0; i < -delta; i++)
  1698.       tputs (term_up, 1, output_character_function);
  1699.     }
  1700.   last_v_pos = to;        /* now to is here */
  1701. }
  1702.  
  1703. /* Physically print C on out_stream.  This is for functions which know
  1704.    how to optimize the display. */
  1705. rl_show_char (c)
  1706.      int c;
  1707. {
  1708.   if (c > 127)
  1709.     {
  1710.       fprintf (out_stream, "M-");
  1711.       c -= 128;
  1712.     }
  1713.  
  1714. #ifdef DISPLAY_TABS
  1715.   if (c < 32 && c != '\t')
  1716. #else
  1717.   if (c < 32)
  1718. #endif
  1719.     {
  1720.  
  1721.       c += 64;
  1722.     }
  1723.  
  1724.   putc (c, out_stream);
  1725.   fflush (out_stream);
  1726. }
  1727.  
  1728. #ifdef DISPLAY_TABS
  1729. int
  1730. rl_character_len (c, pos)
  1731.      register int c, pos;
  1732. {
  1733.   if (c < ' ' || c > 126)
  1734.     {
  1735.       if (c == '\t')
  1736.     return (((pos | (int)7) + 1) - pos);
  1737.       else
  1738.     return (3);
  1739.     }
  1740.   else
  1741.     return (1);
  1742. }
  1743. #else
  1744. int
  1745. rl_character_len (c)
  1746.      int c;
  1747. {
  1748.   if (c < ' ' || c > 126)
  1749.     return (3);
  1750.   else
  1751.     return (1);
  1752. }
  1753. #endif  /* DISPLAY_TAB */
  1754.  
  1755. /* How to print things in the "echo-area".  The prompt is treated as a
  1756.    mini-modeline. */
  1757. rl_message (string, arg1, arg2)
  1758.      char *string;
  1759. {
  1760.   sprintf (msg_buf, string, arg1, arg2);
  1761.   rl_display_prompt = msg_buf;
  1762.   rl_redisplay ();
  1763. }
  1764.  
  1765. /* How to clear things from the "echo-area". */
  1766. rl_clear_message ()
  1767. {
  1768.   rl_display_prompt = rl_prompt;
  1769.   rl_redisplay ();
  1770. }
  1771.  
  1772. /* **************************************************************** */
  1773. /*                                    */
  1774. /*            Terminal and Termcap                */
  1775. /*                                    */
  1776. /* **************************************************************** */
  1777.  
  1778. static char *term_buffer = (char *)NULL;
  1779. static char *term_string_buffer = (char *)NULL;
  1780.  
  1781. /* Non-zero means this terminal can't really do anything. */
  1782. int dumb_term = 0;
  1783.  
  1784. char PC;
  1785. char *BC, *UP;
  1786.  
  1787. /* Some strings to control terminal actions.  These are output by tputs (). */
  1788. char *term_goto, *term_clreol, *term_cr, *term_clrpag, *term_backspace;
  1789.  
  1790. int screenwidth, screenheight;
  1791.  
  1792. /* Non-zero if we determine that the terminal can do character insertion. */
  1793. int terminal_can_insert = 0;
  1794.  
  1795. /* How to insert characters. */
  1796. char *term_im, *term_ei, *term_ic, *term_ip, *term_IC;
  1797.  
  1798. /* How to delete characters. */
  1799. char *term_dc, *term_DC;
  1800.  
  1801. #ifdef HACK_TERMCAP_MOTION
  1802. char *term_forward_char;
  1803. #endif  /* HACK_TERMCAP_MOTION */
  1804.  
  1805. /* How to go up a line. */
  1806. char *term_up;
  1807.  
  1808. /* Re-initialize the terminal considering that the TERM/TERMCAP variable
  1809.    has changed. */
  1810. rl_reset_terminal (terminal_name)
  1811.      char *terminal_name;
  1812. {
  1813.   init_terminal_io (terminal_name);
  1814. }
  1815.  
  1816. init_terminal_io (terminal_name)
  1817.      char *terminal_name;
  1818. {
  1819.   char *term = (terminal_name? terminal_name : (char *)getenv ("TERM"));
  1820.   char *tgetstr (), *buffer;
  1821.  
  1822.  
  1823.   if (!term_string_buffer)
  1824.     term_string_buffer = (char *)xmalloc (2048);
  1825.  
  1826.   if (!term_buffer)
  1827.     term_buffer = (char *)xmalloc (2048);
  1828.  
  1829.   buffer = term_string_buffer;
  1830.  
  1831.   term_clrpag = term_cr = term_clreol = (char *)NULL;
  1832.  
  1833.   if (!term)
  1834.     term = "dumb";
  1835.  
  1836.   if (tgetent (term_buffer, term) < 0)
  1837.     {
  1838.       dumb_term = 1;
  1839.       return;
  1840.     }
  1841.  
  1842.   PC = tgetstr ("pc", &buffer)? *buffer : 0;
  1843.  
  1844.   term_backspace = tgetstr ("le", &buffer);
  1845.  
  1846.   term_cr = tgetstr ("cr", &buffer);
  1847.   term_clreol = tgetstr ("ce", &buffer);
  1848.   term_clrpag = tgetstr ("cl", &buffer);
  1849.  
  1850.   if (!term_cr)
  1851.     term_cr =  "\r";
  1852.  
  1853. #ifdef HACK_TERMCAP_MOTION
  1854.   term_forward_char = tgetstr ("nd", &buffer);
  1855. #endif  /* HACK_TERMCAP_MOTION */
  1856.  
  1857.   screenwidth = tgetnum ("co");
  1858.   if (screenwidth <= 0)
  1859.     screenwidth = 80;
  1860.   screenwidth--;        /* PWP: avoid autowrap bugs */
  1861.  
  1862.   screenheight = tgetnum ("li");
  1863.   if (screenheight <= 0)
  1864.     screenheight = 24;
  1865.  
  1866.   term_im = tgetstr ("im", &buffer);
  1867.   term_ei = tgetstr ("ei", &buffer);
  1868.   term_IC = tgetstr ("IC", &buffer);
  1869.   term_ic = tgetstr ("ic", &buffer);
  1870.  
  1871.   /* "An application program can assume that the terminal can do
  1872.       character insertion if *any one of* the capabilities `IC',
  1873.       `im', `ic' or `ip' is provided."  But we can't do anything if
  1874.       only `ip' is provided, so... */
  1875.   terminal_can_insert = (term_IC || term_im || term_ic);
  1876.  
  1877.   term_up = tgetstr ("up", &buffer);
  1878.   term_dc = tgetstr ("dc", &buffer);
  1879.   term_DC = tgetstr ("DC", &buffer);
  1880. }
  1881.  
  1882. /* A function for the use of tputs () */
  1883. static void
  1884. output_character_function (c)
  1885.      int c;
  1886. {
  1887.   putc (c, out_stream);
  1888. }
  1889.  
  1890. /* Write COUNT characters from STRING to the output stream. */
  1891. static void
  1892. output_some_chars (string, count)
  1893.      char *string;
  1894.      int count;
  1895. {
  1896.   fwrite (string, 1, count, out_stream);
  1897. }
  1898.  
  1899.  
  1900. /* Delete COUNT characters from the display line. */
  1901. static
  1902. delete_chars (count)
  1903.      int count;
  1904. {
  1905.   if (count > screenwidth)
  1906.     return;
  1907.  
  1908.   if (term_DC && *term_DC)
  1909.     {
  1910.       char *tgoto (), *buffer;
  1911.       buffer = tgoto (term_DC, 0, count);
  1912.       tputs (buffer, 1, output_character_function);
  1913.     }
  1914.   else
  1915.     {
  1916.       if (term_dc && *term_dc)
  1917.     while (count--)
  1918.       tputs (term_dc, 1, output_character_function);
  1919.     }
  1920. }
  1921.  
  1922. /* Insert COUNT character from STRING to the output stream. */
  1923. static
  1924. insert_some_chars (string, count)
  1925.      char *string;
  1926.      int count;
  1927. {
  1928.   /* If IC is defined, then we do not have to "enter" insert mode. */
  1929.   if (term_IC)
  1930.     {
  1931.       char *tgoto (), *buffer;
  1932.       buffer = tgoto (term_IC, 0, count);
  1933.       tputs (buffer, 1, output_character_function);
  1934.       output_some_chars (string, count);
  1935.     }
  1936.   else
  1937.     {
  1938.       register int i;
  1939.  
  1940.       /* If we have to turn on insert-mode, then do so. */
  1941.       if (term_im && *term_im)
  1942.     tputs (term_im, 1, output_character_function);
  1943.  
  1944.       /* If there is a special command for inserting characters, then
  1945.      use that first to open up the space. */
  1946.       if (term_ic && *term_ic)
  1947.     {
  1948.       for (i = count; i--; )
  1949.         tputs (term_ic, 1, output_character_function);
  1950.     }
  1951.  
  1952.       /* Print the text. */
  1953.       output_some_chars (string, count);
  1954.  
  1955.       /* If there is a string to turn off insert mode, we had best use
  1956.      it now. */
  1957.       if (term_ei && *term_ei)
  1958.     tputs (term_ei, 1, output_character_function);
  1959.     }
  1960. }
  1961.  
  1962. /* Move the cursor back. */
  1963. backspace (count)
  1964.      int count;
  1965. {
  1966.   register int i;
  1967.  
  1968.   if (term_backspace)
  1969.     for (i = 0; i < count; i++)
  1970.       tputs (term_backspace, 1, output_character_function);
  1971.   else
  1972.     for (i = 0; i < count; i++)
  1973.       putc ('\b', out_stream);
  1974. }
  1975.  
  1976. /* Move to the start of the next line. */
  1977. crlf ()
  1978. {
  1979.   tputs (term_cr, 1, output_character_function);
  1980.   putc ('\n', out_stream);
  1981. }
  1982.  
  1983. /* Clear to the end of the line.  COUNT is the minimum
  1984.    number of character spaces to clear, */
  1985. clear_to_eol (count)
  1986.      int count;
  1987. {
  1988.   if (term_clreol)
  1989.     {
  1990.       tputs (term_clreol, 1, output_character_function);
  1991.     }
  1992.   else
  1993.     {
  1994.       register int i;
  1995.  
  1996.       /* Do one more character space. */
  1997.       count++;
  1998.  
  1999.       for (i = 0; i < count; i++)
  2000.     putc (' ', out_stream);
  2001.  
  2002.       backspace (count);
  2003.     }
  2004. }
  2005.  
  2006.  
  2007. /* **************************************************************** */
  2008. /*                                    */
  2009. /*              Saving and Restoring the TTY                */
  2010. /*                                    */
  2011. /* **************************************************************** */
  2012.  
  2013. /* Non-zero means that the terminal is in a prepped state. */
  2014. static int terminal_prepped = 0;
  2015.  
  2016. #ifdef NEW_TTY_DRIVER
  2017.  
  2018. /* Standard flags, including ECHO. */
  2019. static int original_tty_flags = 0;
  2020.  
  2021. /* Local mode flags, like LPASS8. */
  2022. static int local_mode_flags = 0;
  2023.  
  2024. /* Terminal characters.  This has C-s and C-q in it. */
  2025. static struct tchars original_tchars;
  2026.  
  2027. /* Local special characters.  This has the interrupt characters in it. */
  2028. static struct ltchars original_ltchars;
  2029.  
  2030. /* We use this to get and set the tty_flags. */
  2031. static struct sgttyb the_ttybuff;
  2032.  
  2033. /* Put the terminal in CBREAK mode so that we can detect key presses. */
  2034. static
  2035. rl_prep_terminal ()
  2036. {
  2037.   int tty = fileno (rl_instream);
  2038.   int oldmask = sigblock (sigmask (SIGINT));
  2039.  
  2040.   if (!terminal_prepped)
  2041.     {
  2042.       /* We always get the latest tty values.  Maybe stty changed them. */
  2043.       ioctl (tty, TIOCGETP, &the_ttybuff);
  2044.       original_tty_flags = the_ttybuff.sg_flags;
  2045.  
  2046.       readline_echoing_p = (original_tty_flags & ECHO);
  2047.  
  2048.     
  2049. #if defined (TIOCLGET)
  2050.       ioctl (tty, TIOCLGET, &local_mode_flags);
  2051. #endif
  2052.  
  2053.       /* If this terminal doesn't care how the 8th bit is used,
  2054.      then we can use it for the meta-key.
  2055.      We check by seeing if BOTH odd and even parity are allowed. */
  2056.       if ((the_ttybuff.sg_flags & ODDP) && (the_ttybuff.sg_flags & EVENP))
  2057.     {
  2058. #ifdef PASS8
  2059.       the_ttybuff.sg_flags |= PASS8;
  2060. #endif
  2061.       /* Hack on local mode flags if we can. */
  2062. #if defined (TIOCLGET) && defined (LPASS8)
  2063.       {
  2064.         int flags;
  2065.         flags = local_mode_flags | LPASS8;
  2066.         ioctl (tty, TIOCLSET, &flags);
  2067.       }
  2068. #endif
  2069.     }
  2070.  
  2071. #ifdef TIOCGETC
  2072.       {
  2073.     struct tchars temp;
  2074.  
  2075.     ioctl (tty, TIOCGETC, &original_tchars);
  2076.     bcopy (&original_tchars, &temp, sizeof (struct tchars));
  2077.  
  2078.     /* Get rid of C-s and C-q.
  2079.        We remember the value of startc (C-q) so that if the terminal is in
  2080.        xoff state, the user can xon it by pressing that character. */
  2081.     xon_char = temp.t_startc;
  2082.     temp.t_stopc = -1;
  2083.     temp.t_startc = -1;
  2084.  
  2085.     /* If there is an XON character, bind it to restart the output. */
  2086.     if (xon_char != -1)
  2087.       rl_bind_key (xon_char, rl_restart_output);
  2088.  
  2089.     /* If there is an EOF char, bind eof_char to it. */
  2090.     if (temp.t_eofc != -1)
  2091.       eof_char = temp.t_eofc;
  2092.  
  2093. #ifdef NEVER
  2094.     /* Get rid of C-\ and C-c. */
  2095.     temp.t_intrc = temp.t_quitc = -1;
  2096. #endif
  2097.  
  2098.     ioctl (tty, TIOCSETC, &temp);
  2099.       }
  2100. #endif /* TIOCGETC */
  2101.  
  2102. #ifdef TIOCGLTC
  2103.       {
  2104.     struct ltchars temp;
  2105.  
  2106.     ioctl (tty, TIOCGLTC, &original_ltchars);
  2107.     bcopy (&original_ltchars, &temp, sizeof (struct ltchars));
  2108.  
  2109.     /* Make the interrupt keys go away.  Just enough to make people happy. */
  2110.     temp.t_dsuspc = -1;    /* C-y */
  2111.     temp.t_lnextc = -1;    /* C-v */
  2112.  
  2113.     ioctl (tty, TIOCSLTC, &temp);
  2114.       }
  2115. #endif /* TIOCGLTC */
  2116.  
  2117.       the_ttybuff.sg_flags &= ~ECHO;
  2118.       the_ttybuff.sg_flags |= CBREAK;
  2119.       ioctl (tty, TIOCSETN, &the_ttybuff);
  2120.  
  2121.       terminal_prepped = 1;
  2122.     }
  2123.   sigsetmask (oldmask);
  2124. }
  2125.  
  2126. /* Restore the terminal to its original state. */
  2127. static
  2128. rl_deprep_terminal ()
  2129. {
  2130.   int tty = fileno (rl_instream);
  2131.   int oldmask = sigblock (sigmask (SIGINT));
  2132.  
  2133.   if (terminal_prepped)
  2134.     {
  2135.       the_ttybuff.sg_flags = original_tty_flags;
  2136.       ioctl (tty, TIOCSETN, &the_ttybuff);
  2137.       readline_echoing_p = 1;
  2138.  
  2139. #if defined (TIOCLGET)
  2140.       ioctl (tty, TIOCLSET, &local_mode_flags);
  2141. #endif
  2142.  
  2143. #ifdef TIOCSLTC
  2144.       ioctl (tty, TIOCSLTC, &original_ltchars);
  2145. #endif
  2146.  
  2147. #ifdef TIOCSETC
  2148.       ioctl (tty, TIOCSETC, &original_tchars);
  2149. #endif
  2150.       terminal_prepped = 0;
  2151.     }
  2152.  
  2153.   sigsetmask (oldmask);
  2154. }
  2155.  
  2156. #else  /* !defined (NEW_TTY_DRIVER) */
  2157.  
  2158. #if !defined (VMIN)
  2159. #define VMIN VEOF
  2160. #endif
  2161.  
  2162. #if !defined (VTIME)
  2163. #define VTIME VEOL
  2164. #endif
  2165.  
  2166. static struct termio otio;
  2167.  
  2168. static
  2169. rl_prep_terminal ()
  2170. {
  2171.   int tty = fileno (rl_instream);
  2172.   struct termio tio;
  2173.  
  2174.   ioctl (tty, TCGETA, &tio);
  2175.   ioctl (tty, TCGETA, &otio);
  2176.  
  2177.   readline_echoing_p = (tio.c_lflag & ECHO);
  2178.  
  2179.   tio.c_lflag &= ~(ICANON|ECHO);
  2180.   tio.c_iflag &= ~(IXON|IXOFF|IXANY|ISTRIP|INPCK);
  2181.  
  2182. #if !defined (HANDLE_SIGNALS)
  2183.   tio.c_lflag &= ~ISIG;
  2184. #endif
  2185.  
  2186.   tio.c_cc[VMIN] = 1;
  2187.   tio.c_cc[VTIME] = 0;
  2188.   ioctl (tty, TCSETAW, &tio);
  2189.   ioctl (tty, TCXONC, 1);    /* Simulate a ^Q. */
  2190. }
  2191.  
  2192. static
  2193. rl_deprep_terminal ()
  2194. {
  2195.   int tty = fileno (rl_instream);
  2196.   ioctl (tty, TCSETAW, &otio);
  2197.   ioctl (tty, TCXONC, 1);    /* Simulate a ^Q. */
  2198. }
  2199. #endif  /* NEW_TTY_DRIVER */
  2200.  
  2201.  
  2202. /* **************************************************************** */
  2203. /*                                    */
  2204. /*            Utility Functions                */
  2205. /*                                    */
  2206. /* **************************************************************** */
  2207.  
  2208. /* Return 0 if C is not a member of the class of characters that belong
  2209.    in words, or 1 if it is. */
  2210.  
  2211. int allow_pathname_alphabetic_chars = 0;
  2212. char *pathname_alphabetic_chars = "/-_=~.#$";
  2213.  
  2214. int
  2215. alphabetic (c)
  2216.      int c;
  2217. {
  2218.   char *rindex ();
  2219.   if (pure_alphabetic (c) || (numeric (c)))
  2220.     return (1);
  2221.  
  2222.   if (allow_pathname_alphabetic_chars)
  2223.     return ((int)rindex (pathname_alphabetic_chars, c));
  2224.   else
  2225.     return (0);
  2226. }
  2227.  
  2228. /* Return non-zero if C is a numeric character. */
  2229. int
  2230. numeric (c)
  2231.      int c;
  2232. {
  2233.   return (c >= '0' && c <= '9');
  2234. }
  2235.  
  2236. /* Ring the terminal bell. */
  2237. int
  2238. ding ()
  2239. {
  2240.   if (readline_echoing_p)
  2241.     {
  2242.       fprintf (stderr, "\007");
  2243.       fflush (stderr);
  2244.     }
  2245.   return (-1);
  2246. }
  2247.  
  2248. /* How to abort things. */
  2249. rl_abort ()
  2250. {
  2251.   ding ();
  2252.   rl_clear_message ();
  2253.   rl_init_argument ();
  2254.   rl_pending_input = 0;
  2255.  
  2256.   defining_kbd_macro = 0;
  2257.   while (executing_macro)
  2258.     pop_executing_macro ();
  2259.  
  2260.   longjmp (readline_top_level, 1);
  2261. }
  2262.  
  2263. /* Return a copy of the string between FROM and TO.
  2264.    FROM is inclusive, TO is not. */
  2265. static char *
  2266. rl_copy (from, to)
  2267.      int from, to;
  2268. {
  2269.   register int length;
  2270.   char *copy;
  2271.  
  2272.   /* Fix it if the caller is confused. */
  2273.   if (from > to) {
  2274.     int t = from;
  2275.     from = to;
  2276.     to = t;
  2277.   }
  2278.  
  2279.   length = to - from;
  2280.   copy = (char *)xmalloc (1 + length);
  2281.   strncpy (copy, the_line + from, length);
  2282.   copy[length] = '\0';
  2283.   return (copy);
  2284. }
  2285.  
  2286.  
  2287. /* **************************************************************** */
  2288. /*                                    */
  2289. /*            Insert and Delete                */
  2290. /*                                    */
  2291. /* **************************************************************** */
  2292.  
  2293.  
  2294. /* Insert a string of text into the line at point.  This is the only
  2295.    way that you should do insertion.  rl_insert () calls this
  2296.    function. */
  2297. rl_insert_text (string)
  2298.      char *string;
  2299. {
  2300.   extern int doing_an_undo;
  2301.   register int i, l = strlen (string);
  2302.   while (rl_end + l >= rl_line_buffer_len)
  2303.     {
  2304.       rl_line_buffer =
  2305.     (char *)xrealloc (rl_line_buffer,
  2306.               rl_line_buffer_len += DEFAULT_BUFFER_SIZE);
  2307.       the_line = rl_line_buffer;
  2308.     }
  2309.  
  2310.   for (i = rl_end; i >= rl_point; i--)
  2311.     the_line[i + l] = the_line[i];
  2312.   strncpy (the_line + rl_point, string, l);
  2313.  
  2314.   /* Remember how to undo this if we aren't undoing something. */
  2315.   if (!doing_an_undo)
  2316.     {
  2317.       /* If possible and desirable, concatenate the undos. */
  2318.       if ((strlen (string) == 1) &&
  2319.       rl_undo_list &&
  2320.       (rl_undo_list->what == UNDO_INSERT) &&
  2321.       (rl_undo_list->end == rl_point) &&
  2322.       (rl_undo_list->end - rl_undo_list->start < 20))
  2323.     rl_undo_list->end++;
  2324.       else
  2325.     rl_add_undo (UNDO_INSERT, rl_point, rl_point + l, (char *)NULL);
  2326.     }
  2327.   rl_point += l;
  2328.   rl_end += l;
  2329.   the_line[rl_end] = '\0';
  2330. }
  2331.  
  2332. /* Delete the string between FROM and TO.  FROM is
  2333.    inclusive, TO is not. */
  2334. rl_delete_text (from, to)
  2335.      int from, to;
  2336. {
  2337.   extern int doing_an_undo;
  2338.   register char *text;
  2339.  
  2340.   /* Fix it if the caller is confused. */
  2341.   if (from > to) {
  2342.     int t = from;
  2343.     from = to;
  2344.     to = t;
  2345.   }
  2346.   text = rl_copy (from, to);
  2347.   strncpy (the_line + from, the_line + to, rl_end - to);
  2348.  
  2349.   /* Remember how to undo this delete. */
  2350.   if (!doing_an_undo)
  2351.     rl_add_undo (UNDO_DELETE, from, to, text);
  2352.   else
  2353.     free (text);
  2354.  
  2355.   rl_end -= (to - from);
  2356.   the_line[rl_end] = '\0';
  2357. }
  2358.  
  2359.  
  2360. /* **************************************************************** */
  2361. /*                                    */
  2362. /*            Readline character functions            */
  2363. /*                                    */
  2364. /* **************************************************************** */
  2365.  
  2366. /* This is not a gap editor, just a stupid line input routine.  No hair
  2367.    is involved in writing any of the functions, and none should be. */
  2368.  
  2369. /* Note that:
  2370.  
  2371.    rl_end is the place in the string that we would place '\0';
  2372.    i.e., it is always safe to place '\0' there.
  2373.  
  2374.    rl_point is the place in the string where the cursor is.  Sometimes
  2375.    this is the same as rl_end.
  2376.  
  2377.    Any command that is called interactively receives two arguments.
  2378.    The first is a count: the numeric arg pased to this command.
  2379.    The second is the key which invoked this command.
  2380. */
  2381.  
  2382.  
  2383. /* **************************************************************** */
  2384. /*                                    */
  2385. /*            Movement Commands                */
  2386. /*                                    */
  2387. /* **************************************************************** */
  2388.  
  2389. /* Note that if you `optimize' the display for these functions, you cannot
  2390.    use said functions in other functions which do not do optimizing display.
  2391.    I.e., you will have to update the data base for rl_redisplay, and you
  2392.    might as well let rl_redisplay do that job. */
  2393.  
  2394. /* Move forward COUNT characters. */
  2395. rl_forward (count)
  2396.      int count;
  2397. {
  2398.   if (count < 0)
  2399.     rl_backward (-count);
  2400.   else
  2401.     while (count)
  2402.       {
  2403. #ifdef VI_MODE
  2404.     if (rl_point == (rl_end - (rl_editing_mode == vi_mode)))
  2405. #else
  2406.     if (rl_point == rl_end)
  2407. #endif
  2408.       {
  2409.         ding ();
  2410.         return;
  2411.       }
  2412.     else
  2413.       rl_point++;
  2414.     --count;
  2415.       }
  2416. }
  2417.  
  2418. /* Move backward COUNT characters. */
  2419. rl_backward (count)
  2420.      int count;
  2421. {
  2422.   if (count < 0)
  2423.     rl_forward (-count);
  2424.   else
  2425.     while (count)
  2426.       {
  2427.     if (!rl_point)
  2428.       {
  2429.         ding ();
  2430.         return;
  2431.       }
  2432.     else
  2433.       --rl_point;
  2434.     --count;
  2435.       }
  2436. }
  2437.  
  2438. /* Move to the beginning of the line. */
  2439. rl_beg_of_line ()
  2440. {
  2441.   rl_point = 0;
  2442. }
  2443.  
  2444. /* Move to the end of the line. */
  2445. rl_end_of_line ()
  2446. {
  2447.   rl_point = rl_end;
  2448. }
  2449.  
  2450. /* Move forward a word.  We do what Emacs does. */
  2451. rl_forward_word (count)
  2452.      int count;
  2453. {
  2454.   int c;
  2455.  
  2456.   if (count < 0)
  2457.     {
  2458.       rl_backward_word (-count);
  2459.       return;
  2460.     }
  2461.  
  2462.   while (count)
  2463.     {
  2464.       if (rl_point == rl_end)
  2465.     return;
  2466.  
  2467.       /* If we are not in a word, move forward until we are in one.
  2468.      Then, move forward until we hit a non-alphabetic character. */
  2469.       c = the_line[rl_point];
  2470.       if (!alphabetic (c))
  2471.     {
  2472.       while (++rl_point < rl_end)
  2473.         {
  2474.           c = the_line[rl_point];
  2475.           if (alphabetic (c)) break;
  2476.         }
  2477.     }
  2478.       if (rl_point == rl_end) return;
  2479.       while (++rl_point < rl_end)
  2480.     {
  2481.       c = the_line[rl_point];
  2482.       if (!alphabetic (c)) break;
  2483.     }
  2484.       --count;
  2485.     }
  2486. }
  2487.  
  2488. /* Move backward a word.  We do what Emacs does. */
  2489. rl_backward_word (count)
  2490.      int count;
  2491. {
  2492.   int c;
  2493.  
  2494.   if (count < 0)
  2495.     {
  2496.       rl_forward_word (-count);
  2497.       return;
  2498.     }
  2499.  
  2500.   while (count)
  2501.     {
  2502.       if (!rl_point)
  2503.     return;
  2504.  
  2505.       /* Like rl_forward_word (), except that we look at the characters
  2506.      just before point. */
  2507.  
  2508.       c = the_line[rl_point - 1];
  2509.       if (!alphabetic (c))
  2510.     {
  2511.       while (--rl_point)
  2512.         {
  2513.           c = the_line[rl_point - 1];
  2514.           if (alphabetic (c)) break;
  2515.         }
  2516.     }
  2517.  
  2518.       while (rl_point)
  2519.     {
  2520.       c = the_line[rl_point - 1];
  2521.       if (!alphabetic (c))
  2522.         break;
  2523.       else --rl_point;
  2524.     }
  2525.       --count;
  2526.     }
  2527. }
  2528.  
  2529. /* Clear the current line.  Numeric argument to C-l does this. */
  2530. rl_refresh_line ()
  2531. {
  2532.   int curr_line = last_c_pos / screenwidth;
  2533.   extern char *term_clreol;
  2534.  
  2535.   move_vert(curr_line);
  2536.   move_cursor_relative (0, the_line);   /* XXX is this right */
  2537.  
  2538.   if (term_clreol)
  2539.     tputs (term_clreol, 1, output_character_function);
  2540.  
  2541.   rl_forced_update_display ();
  2542.   rl_display_fixed = 1;
  2543. }
  2544.  
  2545. /* C-l typed to a line without quoting clears the screen, and then reprints
  2546.    the prompt and the current input line.  Given a numeric arg, redraw only
  2547.    the current line. */
  2548. rl_clear_screen ()
  2549. {
  2550.   extern char *term_clrpag;
  2551.  
  2552.   if (rl_explicit_arg)
  2553.     {
  2554.       rl_refresh_line ();
  2555.       return;
  2556.     }
  2557.  
  2558.   if (term_clrpag)
  2559.     tputs (term_clrpag, 1, output_character_function);
  2560.   else
  2561.     crlf ();
  2562.  
  2563.   rl_forced_update_display ();
  2564.   rl_display_fixed = 1;
  2565. }
  2566.  
  2567.  
  2568. /* **************************************************************** */
  2569. /*                                    */
  2570. /*            Text commands                    */
  2571. /*                                    */
  2572. /* **************************************************************** */
  2573.  
  2574. /* Insert the character C at the current location, moving point forward. */
  2575. rl_insert (count, c)
  2576.      int count, c;
  2577. {
  2578.   register int i;
  2579.   char *string;
  2580.  
  2581.   if (count <= 0)
  2582.     return;
  2583.  
  2584.   /* If we can optimize, then do it.  But don't let people crash
  2585.      readline because of extra large arguments. */
  2586.   if (count > 1 && count < 1024)
  2587.     {
  2588.       string = (char *)alloca (1 + count);
  2589.  
  2590.       for (i = 0; i < count; i++)
  2591.     string[i] = c;
  2592.  
  2593.       string[i] = '\0';
  2594.       rl_insert_text (string);
  2595.       return;
  2596.     }
  2597.  
  2598.   if (count > 1024)
  2599.     {
  2600.       int decreaser;
  2601.  
  2602.       string = (char *)alloca (1024 + 1);
  2603.  
  2604.       for (i = 0; i < 1024; i++)
  2605.     string[i] = c;
  2606.  
  2607.       while (count)
  2608.     {
  2609.       decreaser = (count > 1024 ? 1024 : count);
  2610.       string[decreaser] = '\0';
  2611.       rl_insert_text (string);
  2612.       count -= decreaser;
  2613.     }
  2614.       return;
  2615.     }
  2616.  
  2617.   /* We are inserting a single character.
  2618.      If there is pending input, then make a string of all of the
  2619.      pending characters that are bound to rl_insert, and insert
  2620.      them all. */
  2621.   if (any_typein)
  2622.     {
  2623.       int key = 0, t;
  2624.  
  2625.       i = 0;
  2626.       string = (char *)alloca (ibuffer_len + 1);
  2627.       string[i++] = c;
  2628.  
  2629.       while ((t = rl_get_char (&key)) &&
  2630.          (keymap[key].type == ISFUNC &&
  2631.           keymap[key].function == rl_insert))
  2632.     string[i++] = key;
  2633.  
  2634.       if (t)
  2635.     rl_unget_char (key);
  2636.  
  2637.       string[i] = '\0';
  2638.       rl_insert_text (string);
  2639.       return;
  2640.     }
  2641.   else
  2642.     {
  2643.       /* Inserting a single character. */
  2644.       string = (char *)alloca (2);
  2645.  
  2646.       string[1] = '\0';
  2647.       string[0] = c;
  2648.       rl_insert_text (string);
  2649.     }
  2650. }
  2651.  
  2652. /* Insert the next typed character verbatim. */
  2653. rl_quoted_insert (count)
  2654.      int count;
  2655. {
  2656.   int c = rl_read_key ();
  2657.   rl_insert (count, c);
  2658. }
  2659.  
  2660. /* Insert a tab character. */
  2661. rl_tab_insert (count)
  2662.      int count;
  2663. {
  2664.   rl_insert (count, '\t');
  2665. }
  2666.  
  2667. /* What to do when a NEWLINE is pressed.  We accept the whole line.
  2668.    KEY is the key that invoked this command.  I guess it could have
  2669.    meaning in the future. */
  2670. rl_newline (count, key)
  2671.      int count, key;
  2672. {
  2673.  
  2674.   rl_done = 1;
  2675.  
  2676. #ifdef VI_MODE
  2677.   {
  2678.     extern int vi_doing_insert;
  2679.     if (vi_doing_insert)
  2680.       {
  2681.     rl_end_undo_group ();
  2682.     vi_doing_insert = 0;
  2683.       }
  2684.   }
  2685. #endif /* VI_MODE */
  2686.  
  2687.   if (readline_echoing_p)
  2688.     {
  2689.       move_vert (vis_botlin);
  2690.       vis_botlin = 0;
  2691.       crlf ();
  2692.       fflush (out_stream);
  2693.       rl_display_fixed++;
  2694.     }
  2695. }
  2696.  
  2697. rl_clean_up_for_exit ()
  2698. {
  2699.   if (readline_echoing_p)
  2700.     {
  2701.       move_vert (vis_botlin);
  2702.       vis_botlin = 0;
  2703.       fflush (out_stream);
  2704.       rl_restart_output ();
  2705.     }
  2706. }
  2707.  
  2708. /* What to do for some uppercase characters, like meta characters,
  2709.    and some characters appearing in emacs_ctlx_keymap.  This function
  2710.    is just a stub, you bind keys to it and the code in rl_dispatch ()
  2711.    is special cased. */
  2712. rl_do_lowercase_version (ignore1, ignore2)
  2713.      int ignore1, ignore2;
  2714. {
  2715. }
  2716.  
  2717. /* Rubout the character behind point. */
  2718. rl_rubout (count)
  2719.      int count;
  2720. {
  2721.   if (count < 0)
  2722.     {
  2723.       rl_delete (-count);
  2724.       return;
  2725.     }
  2726.  
  2727.   if (!rl_point)
  2728.     {
  2729.       ding ();
  2730.       return;
  2731.     }
  2732.  
  2733.   if (count > 1)
  2734.     {
  2735.       int orig_point = rl_point;
  2736.       rl_backward (count);
  2737.       rl_kill_text (orig_point, rl_point);
  2738.     }
  2739.   else
  2740.     {
  2741.       int c = the_line[--rl_point];
  2742.       rl_delete_text (rl_point, rl_point + 1);
  2743.  
  2744.       if (rl_point == rl_end && alphabetic (c) && last_c_pos)
  2745.     {
  2746.       backspace (1);
  2747.       putc (' ', out_stream);
  2748.       backspace (1);
  2749.       last_c_pos--;
  2750.       visible_line[last_c_pos] = '\0';
  2751.       rl_display_fixed++;
  2752.     }
  2753.     }
  2754. }
  2755.  
  2756. /* Delete the character under the cursor.  Given a numeric argument,
  2757.    kill that many characters instead. */
  2758. rl_delete (count, invoking_key)
  2759.      int count, invoking_key;
  2760. {
  2761.   if (count < 0)
  2762.     {
  2763.       rl_rubout (-count);
  2764.       return;
  2765.     }
  2766.  
  2767.   if (rl_point == rl_end)
  2768.     {
  2769.       ding ();
  2770.       return;
  2771.     }
  2772.  
  2773.   if (count > 1)
  2774.     {
  2775.       int orig_point = rl_point;
  2776.       rl_forward (count);
  2777.       rl_kill_text (orig_point, rl_point);
  2778.       rl_point = orig_point;
  2779.     }
  2780.   else
  2781.     rl_delete_text (rl_point, rl_point + 1);
  2782. }
  2783.  
  2784.  
  2785. /* **************************************************************** */
  2786. /*                                    */
  2787. /*            Kill commands                    */
  2788. /*                                    */
  2789. /* **************************************************************** */
  2790.  
  2791. /* The next two functions mimic unix line editing behaviour, except they
  2792.    save the deleted text on the kill ring.  This is safer than not saving
  2793.    it, and since we have a ring, nobody should get screwed. */
  2794.  
  2795. /* This does what C-w does in Unix.  We can't prevent people from
  2796.    using behaviour that they expect. */
  2797. rl_unix_word_rubout ()
  2798. {
  2799.   if (!rl_point) ding ();
  2800.   else {
  2801.     int orig_point = rl_point;
  2802.     while (rl_point && whitespace (the_line[rl_point - 1]))
  2803.       rl_point--;
  2804.     while (rl_point && !whitespace (the_line[rl_point - 1]))
  2805.       rl_point--;
  2806.     rl_kill_text (rl_point, orig_point);
  2807.   }
  2808. }
  2809.  
  2810. /* Here is C-u doing what Unix does.  You don't *have* to use these
  2811.    key-bindings.  We have a choice of killing the entire line, or
  2812.    killing from where we are to the start of the line.  We choose the
  2813.    latter, because if you are a Unix weenie, then you haven't backspaced
  2814.    into the line at all, and if you aren't, then you know what you are
  2815.    doing. */
  2816. rl_unix_line_discard ()
  2817. {
  2818.   if (!rl_point) ding ();
  2819.   else {
  2820.     rl_kill_text (rl_point, 0);
  2821.     rl_point = 0;
  2822.   }
  2823. }
  2824.  
  2825.  
  2826.  
  2827. /* **************************************************************** */
  2828. /*                                    */
  2829. /*            Commands For Typos                */
  2830. /*                                    */
  2831. /* **************************************************************** */
  2832.  
  2833. /* Random and interesting things in here.  */
  2834.  
  2835.  
  2836. /* **************************************************************** */
  2837. /*                                    */
  2838. /*            Changing Case                    */
  2839. /*                                    */
  2840. /* **************************************************************** */
  2841.  
  2842. /* The three kinds of things that we know how to do. */
  2843. #define UpCase 1
  2844. #define DownCase 2
  2845. #define CapCase 3
  2846.  
  2847. /* Uppercase the word at point. */
  2848. rl_upcase_word (count)
  2849.      int count;
  2850. {
  2851.   rl_change_case (count, UpCase);
  2852. }
  2853.  
  2854. /* Lowercase the word at point. */
  2855. rl_downcase_word (count)
  2856.      int count;
  2857. {
  2858.   rl_change_case (count, DownCase);
  2859. }
  2860.  
  2861. /* Upcase the first letter, downcase the rest. */
  2862. rl_capitalize_word (count)
  2863.      int count;
  2864. {
  2865.   rl_change_case (count, CapCase);
  2866. }
  2867.  
  2868. /* The meaty function.
  2869.    Change the case of COUNT words, performing OP on them.
  2870.    OP is one of UpCase, DownCase, or CapCase.
  2871.    If a negative argument is given, leave point where it started,
  2872.    otherwise, leave it where it moves to. */
  2873. rl_change_case (count, op)
  2874.      int count, op;
  2875. {
  2876.   register int start = rl_point, end;
  2877.   int state = 0;
  2878.  
  2879.   rl_forward_word (count);
  2880.   end = rl_point;
  2881.  
  2882.   if (count < 0)
  2883.     {
  2884.       int temp = start;
  2885.       start = end;
  2886.       end = temp;
  2887.     }
  2888.  
  2889.   /* We are going to modify some text, so let's prepare to undo it. */
  2890.   rl_modifying (start, end);
  2891.  
  2892.   for (; start < end; start++)
  2893.     {
  2894.       switch (op)
  2895.     {
  2896.     case UpCase:
  2897.       the_line[start] = to_upper (the_line[start]);
  2898.       break;
  2899.  
  2900.     case DownCase:
  2901.       the_line[start] = to_lower (the_line[start]);
  2902.       break;
  2903.  
  2904.     case CapCase:
  2905.       if (state == 0)
  2906.         {
  2907.           the_line[start] = to_upper (the_line[start]);
  2908.           state = 1;
  2909.         }
  2910.       else
  2911.         {
  2912.           the_line[start] = to_lower (the_line[start]);
  2913.         }
  2914.       if (!pure_alphabetic (the_line[start]))
  2915.         state = 0;
  2916.       break;
  2917.  
  2918.     default:
  2919.       abort ();
  2920.     }
  2921.     }
  2922.   rl_point = end;
  2923. }
  2924.  
  2925. /* **************************************************************** */
  2926. /*                                    */
  2927. /*            Transposition                    */
  2928. /*                                    */
  2929. /* **************************************************************** */
  2930.  
  2931. /* Transpose the words at point. */
  2932. rl_transpose_words (count)
  2933.      int count;
  2934. {
  2935.   char *word1, *word2;
  2936.   int w1_beg, w1_end, w2_beg, w2_end;
  2937.   int orig_point = rl_point;
  2938.  
  2939.   if (!count) return;
  2940.  
  2941.   /* Find the two words. */
  2942.   rl_forward_word (count);
  2943.   w2_end = rl_point;
  2944.   rl_backward_word (1);
  2945.   w2_beg = rl_point;
  2946.   rl_backward_word (count);
  2947.   w1_beg = rl_point;
  2948.   rl_forward_word (1);
  2949.   w1_end = rl_point;
  2950.  
  2951.   /* Do some check to make sure that there really are two words. */
  2952.   if ((w1_beg == w2_beg) || (w2_beg < w1_end))
  2953.     {
  2954.       ding ();
  2955.       rl_point = orig_point;
  2956.       return;
  2957.     }
  2958.  
  2959.   /* Get the text of the words. */
  2960.   word1 = rl_copy (w1_beg, w1_end);
  2961.   word2 = rl_copy (w2_beg, w2_end);
  2962.  
  2963.   /* We are about to do many insertions and deletions.  Remember them
  2964.      as one operation. */
  2965.   rl_begin_undo_group ();
  2966.  
  2967.   /* Do the stuff at word2 first, so that we don't have to worry
  2968.      about word1 moving. */
  2969.   rl_point = w2_beg;
  2970.   rl_delete_text (w2_beg, w2_end);
  2971.   rl_insert_text (word1);
  2972.  
  2973.   rl_point = w1_beg;
  2974.   rl_delete_text (w1_beg, w1_end);
  2975.   rl_insert_text (word2);
  2976.  
  2977.   /* This is exactly correct since the text before this point has not
  2978.      changed in length. */
  2979.   rl_point = w2_end;
  2980.  
  2981.   /* I think that does it. */
  2982.   rl_end_undo_group ();
  2983.   free (word1); free (word2);
  2984. }
  2985.  
  2986. /* Transpose the characters at point.  If point is at the end of the line,
  2987.    then transpose the characters before point. */
  2988. rl_transpose_chars (count)
  2989.      int count;
  2990. {
  2991.   if (!count)
  2992.     return;
  2993.  
  2994.   if (!rl_point || rl_end < 2) {
  2995.     ding ();
  2996.     return;
  2997.   }
  2998.  
  2999.   while (count) {
  3000.     if (rl_point == rl_end) {
  3001.       int t = the_line[rl_point - 1];
  3002.       the_line[rl_point - 1] = the_line[rl_point - 2];
  3003.       the_line[rl_point - 2] = t;
  3004.     } else {
  3005.       int t = the_line[rl_point];
  3006.       the_line[rl_point] = the_line[rl_point - 1];
  3007.       the_line[rl_point - 1] = t;
  3008.       if (count < 0 && rl_point)
  3009.     rl_point--;
  3010.       else
  3011.     rl_point++;
  3012.     }
  3013.     if (count < 0)
  3014.       count++;
  3015.     else
  3016.       count--;
  3017.   }
  3018. }
  3019.  
  3020.  
  3021. /* **************************************************************** */
  3022. /*                                    */
  3023. /*            Bogus Flow Control                  */
  3024. /*                                    */
  3025. /* **************************************************************** */
  3026.  
  3027. rl_restart_output (count, key)
  3028.      int count, key;
  3029. {
  3030.   int fildes = fileno (stdin);
  3031. #ifdef TIOCSTART
  3032.   ioctl (fildes, TIOCSTART, 0);
  3033. #endif /* TIOCSTART */
  3034. }
  3035.  
  3036. /* **************************************************************** */
  3037. /*                                    */
  3038. /*    Completion matching, from readline's point of view.        */
  3039. /*                                    */
  3040. /* **************************************************************** */
  3041.  
  3042. /* Pointer to the generator function for completion_matches ().
  3043.    NULL means to use filename_entry_function (), the default filename
  3044.    completer. */
  3045. Function *rl_completion_entry_function = (Function *)NULL;
  3046.  
  3047. /* Pointer to alternative function to create matches.
  3048.    Function is called with TEXT, START, and END.
  3049.    START and END are indices in RL_LINE_BUFFER saying what the boundaries
  3050.    of TEXT are.
  3051.    If this function exists and returns NULL then call the value of
  3052.    rl_completion_entry_function to try to match, otherwise use the
  3053.    array of strings returned. */
  3054. Function *rl_attempted_completion_function = (Function *)NULL;
  3055.  
  3056. /* Complete the word at or before point.  You have supplied the function
  3057.    that does the initial simple matching selection algorithm (see
  3058.    completion_matches ()).  The default is to do filename completion. */
  3059. rl_complete (ignore, invoking_key)
  3060.      int ignore, invoking_key;
  3061. {
  3062.   rl_complete_internal (TAB);
  3063. }
  3064.  
  3065. /* List the possible completions.  See description of rl_complete (). */
  3066. rl_possible_completions ()
  3067. {
  3068.   rl_complete_internal ('?');
  3069. }
  3070.  
  3071. /* The user must press "y" or "n". Non-zero return means "y" pressed. */
  3072. get_y_or_n ()
  3073. {
  3074.   int c;
  3075.  loop:
  3076.   c = rl_read_key ();
  3077.   if (c == 'y' || c == 'Y') return (1);
  3078.   if (c == 'n' || c == 'N') return (0);
  3079.   if (c == ABORT_CHAR) rl_abort ();
  3080.   ding (); goto loop;
  3081. }
  3082.  
  3083. /* Up to this many items will be displayed in response to a
  3084.    possible-completions call.  After that, we ask the user if
  3085.    she is sure she wants to see them all. */
  3086. int rl_completion_query_items = 100;
  3087.  
  3088. /* The basic list of characters that signal a break between words for the
  3089.    completer routine.  The contents of this variable is what breaks words
  3090.    in the shell, i.e. " \t\n\"\\'`@$><=" */
  3091. char *rl_basic_word_break_characters = " \t\n\"\\'`@$><=";
  3092.  
  3093. /* The list of characters that signal a break between words for
  3094.    rl_complete_internal.  The default list is the contents of
  3095.    rl_basic_word_break_characters.  */
  3096. char *rl_completer_word_break_characters = (char *)NULL;
  3097.  
  3098. /* List of characters that are word break characters, but should be left
  3099.    in TEXT when it is passed to the completion function.  The shell uses
  3100.    this to help determine what kind of completing to do. */
  3101. char *rl_special_prefixes = (char *)NULL;
  3102.  
  3103. /* If non-zero, then disallow duplicates in the matches. */
  3104. int rl_ignore_completion_duplicates = 1;
  3105.  
  3106. /* Non-zero means that the results of the matches are to be treated
  3107.    as filenames.  This is ALWAYS zero on entry, and can only be changed
  3108.    within a completion entry finder function. */
  3109. int rl_filename_completion_desired = 0;
  3110.  
  3111. /* Complete the word at or before point.
  3112.    WHAT_TO_DO says what to do with the completion.
  3113.    `?' means list the possible completions.
  3114.    TAB means do standard completion.
  3115.    `*' means insert all of the possible completions. */
  3116. rl_complete_internal (what_to_do)
  3117.      int what_to_do;
  3118. {
  3119.   char *filename_completion_function ();
  3120.   char **completion_matches (), **matches;
  3121.   Function *our_func;
  3122.   int start, end, delimiter = 0;
  3123.   char *text;
  3124.  
  3125.   if (rl_completion_entry_function)
  3126.     our_func = rl_completion_entry_function;
  3127.   else
  3128.     our_func = (int (*)())filename_completion_function;
  3129.  
  3130.   /* Only the completion entry function can change this. */
  3131.   rl_filename_completion_desired = 0;
  3132.  
  3133.   /* We now look backwards for the start of a filename/variable word. */
  3134.   end = rl_point;
  3135.   if (rl_point)
  3136.     {
  3137.       while (--rl_point &&
  3138.          !rindex (rl_completer_word_break_characters, the_line[rl_point]));
  3139.  
  3140.       /* If we are at a word break, then advance past it. */
  3141.       if (rindex (rl_completer_word_break_characters,  (the_line[rl_point])))
  3142.     {
  3143.       /* If the character that caused the word break was a quoting
  3144.          character, then remember it as the delimiter. */
  3145.       if (rindex ("\"'", the_line[rl_point]) && (end - rl_point) > 1)
  3146.         delimiter = the_line[rl_point];
  3147.  
  3148.       /* If the character isn't needed to determine something special
  3149.          about what kind of completion to perform, then advance past it. */
  3150.  
  3151.       if (!rl_special_prefixes ||
  3152.           !rindex (rl_special_prefixes, the_line[rl_point]))
  3153.         rl_point++;
  3154.     }
  3155.     }
  3156.  
  3157.   start = rl_point;
  3158.   rl_point = end;
  3159.   text = rl_copy (start, end);
  3160.  
  3161.   /* If the user wants to TRY to complete, but then wants to give
  3162.      up and use the default completion function, they set the
  3163.      variable rl_attempted_completion_function. */
  3164.   if (rl_attempted_completion_function)
  3165.     {
  3166.       matches =
  3167.     (char **)(*rl_attempted_completion_function) (text, start, end);
  3168.  
  3169.       if (matches)
  3170.     goto after_usual_completion;
  3171.     }
  3172.  
  3173.   matches = completion_matches (text, our_func, start, end);
  3174.  
  3175.  after_usual_completion:
  3176.   free (text);
  3177.  
  3178.   if (!matches)
  3179.     ding ();
  3180.   else
  3181.     {
  3182.       register int i;
  3183.  
  3184.     some_matches:
  3185.  
  3186.       /* It seems to me that in all the cases we handle we would like
  3187.      to ignore duplicate possiblilities.  Scan for the text to
  3188.      insert being identical to the other completions. */
  3189.       if (rl_ignore_completion_duplicates)
  3190.     {
  3191.       char *lowest_common;
  3192.       int j, newlen = 0;
  3193.  
  3194.       /* Sort the items. */
  3195.       /* It is safe to sort this array, because the lowest common
  3196.          denominator found in matches[0] will remain in place. */
  3197.       for (i = 0; matches[i]; i++);
  3198.       qsort (matches, i, sizeof (char *), compare_strings);
  3199.  
  3200.       /* Remember the lowest common denimator for it may be unique. */
  3201.       lowest_common = savestring (matches[0]);
  3202.  
  3203.       for (i = 0; matches[i + 1]; i++)
  3204.         {
  3205.           if (strcmp (matches[i], matches[i + 1]) == 0)
  3206.         {
  3207.           free (matches[i]);
  3208.           matches[i] = (char *)-1;
  3209.         }
  3210.           else
  3211.         newlen++;
  3212.         }
  3213.  
  3214.       /* We have marked all the dead slots with (char *)-1.
  3215.          Copy all the non-dead entries into a new array. */
  3216.       {
  3217.         char **temp_array =
  3218.           (char **)malloc ((3 + newlen) * sizeof (char *));
  3219.  
  3220.         for (i = 1, j = 1; matches[i]; i++)
  3221.           if (matches[i] != (char *)-1)
  3222.         temp_array[j++] = matches[i];
  3223.         temp_array[j] = (char *)NULL;
  3224.  
  3225.         if (matches[0] != (char *)-1)
  3226.           free (matches[0]);
  3227.         free (matches);
  3228.  
  3229.         matches = temp_array;
  3230.       }
  3231.  
  3232.       /* Place the lowest common denominator back in [0]. */
  3233.       matches[0] = lowest_common;
  3234.  
  3235.       /* If there is one string left, and it is identical to the
  3236.          lowest common denominator, then the LCD is the string to
  3237.          insert. */
  3238.       if (j == 2 && strcmp (matches[0], matches[1]) == 0)
  3239.         {
  3240.           free (matches[1]);
  3241.           matches[1] = (char *)NULL;
  3242.         }
  3243.     }
  3244.  
  3245.       switch (what_to_do)
  3246.     {
  3247.     case TAB:
  3248.       if (matches[0])
  3249.         {
  3250.           rl_delete_text (start, rl_point);
  3251.           rl_point = start;
  3252.           rl_insert_text (matches[0]);
  3253.         }
  3254.  
  3255.       /* If there are more matches, ring the bell to indicate.
  3256.          If this was the only match, and we are hacking files,
  3257.          check the file to see if it was a directory.  If so,
  3258.          add a '/' to the name.  If not, and we are at the end
  3259.          of the line, then add a space. */
  3260.       if (matches[1])
  3261.         {
  3262.           ding ();        /* There are other matches remaining. */
  3263.         }
  3264.       else
  3265.         {
  3266.           char temp_string[2];
  3267.  
  3268.           temp_string[0] = delimiter ? delimiter : ' ';
  3269.           temp_string[1] = '\0';
  3270.  
  3271.           if (rl_filename_completion_desired)
  3272.         {
  3273.           struct stat finfo;
  3274.           char *tilde_expand ();
  3275.           char *filename = tilde_expand (matches[0]);
  3276.  
  3277.           if ((stat (filename, &finfo) == 0) &&
  3278.               ((finfo.st_mode & S_IFMT) == S_IFDIR))
  3279.             {
  3280.               if (the_line[rl_point] != '/')
  3281.             rl_insert_text ("/");
  3282.             }
  3283.           else
  3284.             {
  3285.               if (rl_point == rl_end)
  3286.             rl_insert_text (temp_string);
  3287.             }
  3288.           free (filename);
  3289.         }
  3290.           else
  3291.         {
  3292.           if (rl_point == rl_end)
  3293.             rl_insert_text (temp_string);
  3294.         }
  3295.         }
  3296.       break;
  3297.  
  3298.     case '*':
  3299.       {
  3300.         int i = 1;
  3301.  
  3302.         rl_delete_text (start, rl_point);
  3303.         rl_point = start;
  3304.         rl_begin_undo_group ();
  3305.         if (matches[1])
  3306.           {
  3307.         while (matches[i])
  3308.           {
  3309.             rl_insert_text (matches[i++]);
  3310.             rl_insert_text (" ");
  3311.           }
  3312.           }
  3313.         else
  3314.           {
  3315.         rl_insert_text (matches[0]);
  3316.         rl_insert_text (" ");
  3317.           }
  3318.         rl_end_undo_group ();
  3319.       }
  3320.       break;
  3321.  
  3322.  
  3323.     case '?':
  3324.       {
  3325.         int len, count, limit, max = 0;
  3326.         int j, k, l;
  3327.  
  3328.         /* Handle simple case first.  What if there is only one answer? */
  3329.         if (!matches[1])
  3330.           {
  3331.         char *rindex (), *temp;
  3332.  
  3333.         if (rl_filename_completion_desired)
  3334.           temp = rindex (matches[0], '/');
  3335.         else
  3336.           temp = (char *)NULL;
  3337.  
  3338.         if (!temp)
  3339.           temp = matches[0];
  3340.         else
  3341.           temp++;
  3342.  
  3343.         crlf ();
  3344.         fprintf (out_stream, "%s", temp);
  3345.         crlf ();
  3346.         goto restart;
  3347.           }
  3348.  
  3349.         /* There is more than one answer.  Find out how many there are,
  3350.            and find out what the maximum printed length of a single entry
  3351.            is. */
  3352.         for (i = 1; matches[i]; i++)
  3353.           {
  3354.         char *rindex (), *temp = (char *)NULL;
  3355.  
  3356.         /* If we are hacking filenames, then only count the characters
  3357.            after the last slash in the pathname. */
  3358.         if (rl_filename_completion_desired)
  3359.           temp = rindex (matches[i], '/');
  3360.         else
  3361.           temp = (char *)NULL;
  3362.  
  3363.         if (!temp)
  3364.           temp = matches[i];
  3365.         else
  3366.           temp++;
  3367.  
  3368.         if (strlen (temp) > max)
  3369.           max = strlen (temp);
  3370.           }
  3371.  
  3372.         len = i;
  3373.  
  3374.         /* If there are many items, then ask the user if she
  3375.            really wants to see them all. */
  3376.         if (len >= rl_completion_query_items)
  3377.           {
  3378.         crlf ();
  3379.         fprintf (out_stream,
  3380.              "There are %d possibilities.  Do you really", len);
  3381.         crlf ();
  3382.         fprintf (out_stream, "wish to see them all? (y or n)");
  3383.         fflush (out_stream);
  3384.         if (!get_y_or_n ())
  3385.           {
  3386.             crlf ();
  3387.             goto restart;
  3388.           }
  3389.           }
  3390.         /* How many items of MAX length can we fit in the screen window? */
  3391.         max += 2;
  3392.         limit = screenwidth / max;
  3393.         if (limit != 1 && (limit * max == screenwidth))
  3394.           limit--;
  3395.  
  3396.         /* How many iterations of the printing loop? */
  3397.         count = (len + (limit - 1)) / limit;
  3398.  
  3399.         /* Watch out for special case.  If LEN is less than LIMIT, then
  3400.            just do the inner printing loop. */
  3401.         if (len < limit) count = 1;
  3402.  
  3403.         /* Sort the items if they are not already sorted. */
  3404.         if (!rl_ignore_completion_duplicates)
  3405.           qsort (matches, len, sizeof (char *), compare_strings);
  3406.  
  3407.         /* Print the sorted items, up-and-down alphabetically, like
  3408.            ls might. */
  3409.         crlf ();
  3410.  
  3411.         for (i = 1; i < count + 1; i++)
  3412.           {
  3413.         for (j = 0, l = i; j < limit; j++)
  3414.           {
  3415.             if (l > len || !matches[l])
  3416.               {
  3417.             break;
  3418.               }
  3419.             else
  3420.               {
  3421.             char *rindex (), *temp = (char *)NULL;
  3422.  
  3423.             if (rl_filename_completion_desired)
  3424.               temp = rindex (matches[l], '/');
  3425.             else
  3426.               temp = (char *)NULL;
  3427.  
  3428.             if (!temp)
  3429.               temp = matches[l];
  3430.             else
  3431.               temp++;
  3432.  
  3433.             fprintf (out_stream, "%s", temp);
  3434.             for (k = 0; k < max - strlen (temp); k++)
  3435.               putc (' ', out_stream);
  3436.               }
  3437.             l += count;
  3438.           }
  3439.         crlf ();
  3440.           }
  3441.       restart:
  3442.  
  3443.         rl_on_new_line ();
  3444.       }
  3445.       break;
  3446.  
  3447.     default:
  3448.       abort ();
  3449.     }
  3450.  
  3451.       for (i = 0; matches[i]; i++)
  3452.     free (matches[i]);
  3453.       free (matches);
  3454.     }
  3455. }
  3456.  
  3457. /* Stupid comparison routine for qsort () ing strings. */
  3458. static int
  3459. compare_strings (s1, s2)
  3460.   char **s1, **s2;
  3461. {
  3462.   return (strcmp (*s1, *s2));
  3463. }
  3464.  
  3465. /* A completion function for usernames.
  3466.    TEXT contains a partial username preceded by a random
  3467.    character (usually `~').  */
  3468. char *
  3469. username_completion_function (text, state)
  3470.      int state;
  3471.      char *text;
  3472. {
  3473.   static char *username = (char *)NULL;
  3474.   static struct passwd *entry;
  3475.   static int namelen;
  3476.  
  3477.   if (!state)
  3478.     {
  3479.       if (username)
  3480.     free (username);
  3481.       username = savestring (&text[1]);
  3482.       namelen = strlen (username);
  3483.       setpwent ();
  3484.     }
  3485.  
  3486.   while (entry = getpwent ())
  3487.     {
  3488.       if (strncmp (username, entry->pw_name, namelen) == 0)
  3489.     break;
  3490.     }
  3491.  
  3492.   if (!entry)
  3493.     {
  3494.       endpwent ();
  3495.       return ((char *)NULL);
  3496.     }
  3497.   else
  3498.     {
  3499.       char *value = (char *)xmalloc (2 + strlen (entry->pw_name));
  3500.       *value = *text;
  3501.       strcpy (value + 1, entry->pw_name);
  3502.       rl_filename_completion_desired = 1;
  3503.       return (value);
  3504.     }
  3505. }
  3506.  
  3507. /* If non-null, this contains the address of a function to call if the
  3508.    standard meaning for expanding a tilde fails.  The function is called
  3509.    with the text (sans tilde, as in "foo"), and returns a malloc()'ed string
  3510.    which is the expansion, or a NULL pointer if there is no expansion. */
  3511. Function *rl_tilde_expander = (Function *)NULL;
  3512.  
  3513. /* Expand FILENAME if it begins with a tilde.  This always returns
  3514.    a new string. */
  3515. char *
  3516. tilde_expand (filename)
  3517.      char *filename;
  3518. {
  3519.   char *dirname = filename ? savestring (filename) : (char *)NULL;
  3520.  
  3521.   if (dirname && *dirname == '~')
  3522.     {
  3523.       char *temp_name;
  3524.       if (!dirname[1] || dirname[1] == '/')
  3525.     {
  3526.       /* Prepend $HOME to the rest of the string. */
  3527.       char *temp_home = (char *)getenv ("HOME");
  3528.  
  3529.       temp_name = (char *)alloca (1 + strlen (&dirname[1])
  3530.                       + (temp_home? strlen (temp_home) : 0));
  3531.       temp_name[0] = '\0';
  3532.       if (temp_home)
  3533.         strcpy (temp_name, temp_home);
  3534.       strcat (temp_name, &dirname[1]);
  3535.       free (dirname);
  3536.       dirname = savestring (temp_name);
  3537.     }
  3538.       else
  3539.     {
  3540.       struct passwd *getpwnam (), *user_entry;
  3541.       char *username = (char *)alloca (257);
  3542.       int i, c;
  3543.  
  3544.       for (i = 1; c = dirname[i]; i++)
  3545.         {
  3546.           if (c == '/') break;
  3547.           else username[i - 1] = c;
  3548.         }
  3549.       username[i - 1] = '\0';
  3550.  
  3551.       if (!(user_entry = getpwnam (username)))
  3552.         {
  3553.           /* If the calling program has a special syntax for
  3554.          expanding tildes, and we couldn't find a standard
  3555.          expansion, then let them try. */
  3556.           if (rl_tilde_expander)
  3557.         {
  3558.           char *expansion;
  3559.  
  3560.           expansion = (char *)(*rl_tilde_expander) (username);
  3561.  
  3562.           if (expansion)
  3563.             {
  3564.               temp_name = (char *)alloca (1 + strlen (expansion)
  3565.                           + strlen (&dirname[i]));
  3566.               strcpy (temp_name, expansion);
  3567.               strcat (temp_name, &dirname[i]);
  3568.               free (expansion);
  3569.               goto return_name;
  3570.             }
  3571.         }
  3572.           /*
  3573.            * We shouldn't report errors.
  3574.            */
  3575.         }
  3576.       else
  3577.         {
  3578.           temp_name = (char *)alloca (1 + strlen (user_entry->pw_dir)
  3579.                       + strlen (&dirname[i]));
  3580.           strcpy (temp_name, user_entry->pw_dir);
  3581.           strcat (temp_name, &dirname[i]);
  3582.         return_name:
  3583.           free (dirname);
  3584.           dirname = savestring (temp_name);
  3585.         }
  3586.     }
  3587.     }
  3588.   return (dirname);
  3589. }
  3590.  
  3591.  
  3592. /* **************************************************************** */
  3593. /*                                    */
  3594. /*            Undo, and Undoing                */
  3595. /*                                    */
  3596. /* **************************************************************** */
  3597.  
  3598. /* Non-zero tells rl_delete_text and rl_insert_text to not add to
  3599.    the undo list. */
  3600. int doing_an_undo = 0;
  3601.  
  3602. /* The current undo list for THE_LINE. */
  3603. UNDO_LIST *rl_undo_list = (UNDO_LIST *)NULL;
  3604.  
  3605. /* Remember how to undo something.  Concatenate some undos if that
  3606.    seems right. */
  3607. rl_add_undo (what, start, end, text)
  3608.      enum undo_code what;
  3609.      int start, end;
  3610.      char *text;
  3611. {
  3612.   UNDO_LIST *temp = (UNDO_LIST *)xmalloc (sizeof (UNDO_LIST));
  3613.   temp->what = what;
  3614.   temp->start = start;
  3615.   temp->end = end;
  3616.   temp->text = text;
  3617.   temp->next = rl_undo_list;
  3618.   rl_undo_list = temp;
  3619. }
  3620.  
  3621. /* Free the existing undo list. */
  3622. free_undo_list ()
  3623. {
  3624.   while (rl_undo_list) {
  3625.     UNDO_LIST *release = rl_undo_list;
  3626.     rl_undo_list = rl_undo_list->next;
  3627.  
  3628.     if (release->what == UNDO_DELETE)
  3629.       free (release->text);
  3630.  
  3631.     free (release);
  3632.   }
  3633. }
  3634.  
  3635. /* Undo the next thing in the list.  Return 0 if there
  3636.    is nothing to undo, or non-zero if there was. */
  3637. int
  3638. rl_do_undo ()
  3639. {
  3640.   UNDO_LIST *release;
  3641.   int waiting_for_begin = 0;
  3642.  
  3643. undo_thing:
  3644.   if (!rl_undo_list)
  3645.     return (0);
  3646.  
  3647.   doing_an_undo = 1;
  3648.  
  3649.   switch (rl_undo_list->what) {
  3650.  
  3651.     /* Undoing deletes means inserting some text. */
  3652.   case UNDO_DELETE:
  3653.     rl_point = rl_undo_list->start;
  3654.     rl_insert_text (rl_undo_list->text);
  3655.     free (rl_undo_list->text);
  3656.     break;
  3657.  
  3658.     /* Undoing inserts means deleting some text. */
  3659.   case UNDO_INSERT:
  3660.     rl_delete_text (rl_undo_list->start, rl_undo_list->end);
  3661.     rl_point = rl_undo_list->start;
  3662.     break;
  3663.  
  3664.     /* Undoing an END means undoing everything 'til we get to
  3665.        a BEGIN. */
  3666.   case UNDO_END:
  3667.     waiting_for_begin++;
  3668.     break;
  3669.  
  3670.     /* Undoing a BEGIN means that we are done with this group. */
  3671.   case UNDO_BEGIN:
  3672.     if (waiting_for_begin)
  3673.       waiting_for_begin--;
  3674.     else
  3675.       abort ();
  3676.     break;
  3677.   }
  3678.  
  3679.   doing_an_undo = 0;
  3680.  
  3681.   release = rl_undo_list;
  3682.   rl_undo_list = rl_undo_list->next;
  3683.   free (release);
  3684.  
  3685.   if (waiting_for_begin)
  3686.     goto undo_thing;
  3687.  
  3688.   return (1);
  3689. }
  3690.  
  3691. /* Begin a group.  Subsequent undos are undone as an atomic operation. */
  3692. rl_begin_undo_group ()
  3693. {
  3694.   rl_add_undo (UNDO_BEGIN, 0, 0, 0);
  3695. }
  3696.  
  3697. /* End an undo group started with rl_begin_undo_group (). */
  3698. rl_end_undo_group ()
  3699. {
  3700.   rl_add_undo (UNDO_END, 0, 0, 0);
  3701. }
  3702.  
  3703. /* Save an undo entry for the text from START to END. */
  3704. rl_modifying (start, end)
  3705.      int start, end;
  3706. {
  3707.   if (start > end)
  3708.     {
  3709.       int t = start;
  3710.       start = end;
  3711.       end = t;
  3712.     }
  3713.  
  3714.   if (start != end)
  3715.     {
  3716.       char *temp = rl_copy (start, end);
  3717.       rl_begin_undo_group ();
  3718.       rl_add_undo (UNDO_DELETE, start, end, temp);
  3719.       rl_add_undo (UNDO_INSERT, start, end, (char *)NULL);
  3720.       rl_end_undo_group ();
  3721.     }
  3722. }
  3723.  
  3724. /* Revert the current line to its previous state. */
  3725. rl_revert_line ()
  3726. {
  3727.   if (!rl_undo_list) ding ();
  3728.   else {
  3729.     while (rl_undo_list)
  3730.       rl_do_undo ();
  3731.   }
  3732. }
  3733.  
  3734. /* Do some undoing of things that were done. */
  3735. rl_undo_command (count)
  3736. {
  3737.   if (count < 0) return;    /* Nothing to do. */
  3738.  
  3739.   while (count)
  3740.     {
  3741.       if (rl_do_undo ())
  3742.     {
  3743.       count--;
  3744.     }
  3745.       else
  3746.     {
  3747.       ding ();
  3748.       break;
  3749.     }
  3750.     }
  3751. }
  3752.  
  3753. /* **************************************************************** */
  3754. /*                                    */
  3755. /*            History Utilities                */
  3756. /*                                    */
  3757. /* **************************************************************** */
  3758.  
  3759. /* We already have a history library, and that is what we use to control
  3760.    the history features of readline.  However, this is our local interface
  3761.    to the history mechanism. */
  3762.  
  3763. /* While we are editing the history, this is the saved
  3764.    version of the original line. */
  3765. HIST_ENTRY *saved_line_for_history = (HIST_ENTRY *)NULL;
  3766.  
  3767. /* Set the history pointer back to the last entry in the history. */
  3768. start_using_history ()
  3769. {
  3770.   using_history ();
  3771.   if (saved_line_for_history)
  3772.     free_history_entry (saved_line_for_history);
  3773.  
  3774.   saved_line_for_history = (HIST_ENTRY *)NULL;
  3775. }
  3776.  
  3777. /* Free the contents (and containing structure) of a HIST_ENTRY. */
  3778. free_history_entry (entry)
  3779.      HIST_ENTRY *entry;
  3780. {
  3781.   if (!entry) return;
  3782.   if (entry->line)
  3783.     free (entry->line);
  3784.   free (entry);
  3785. }
  3786.  
  3787. /* Perhaps put back the current line if it has changed. */
  3788. maybe_replace_line ()
  3789. {
  3790.   HIST_ENTRY *temp = current_history ();
  3791.  
  3792.   /* If the current line has changed, save the changes. */
  3793.   if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list)) {
  3794.     temp = replace_history_entry (where_history (), the_line, rl_undo_list);
  3795.     free (temp->line);
  3796.     free (temp);
  3797.   }
  3798. }
  3799.  
  3800. /* Put back the saved_line_for_history if there is one. */
  3801. maybe_unsave_line ()
  3802. {
  3803.   if (saved_line_for_history) {
  3804.     strcpy (the_line, saved_line_for_history->line);
  3805.     rl_undo_list = (UNDO_LIST *)saved_line_for_history->data;
  3806.     free_history_entry (saved_line_for_history);
  3807.     saved_line_for_history = (HIST_ENTRY *)NULL;
  3808.     rl_end = rl_point = strlen (the_line);
  3809.   } else {
  3810.     ding ();
  3811.   }
  3812. }
  3813.  
  3814. /* Save the current line in saved_line_for_history. */
  3815. maybe_save_line ()
  3816. {
  3817.   if (!saved_line_for_history) {
  3818.     saved_line_for_history = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
  3819.     saved_line_for_history->line = savestring (the_line);
  3820.     saved_line_for_history->data = (char *)rl_undo_list;
  3821.   }
  3822. }
  3823.  
  3824.  
  3825.  
  3826. /* **************************************************************** */
  3827. /*                                    */
  3828. /*            History Commands                */
  3829. /*                                    */
  3830. /* **************************************************************** */
  3831.  
  3832. /* Meta-< goes to the start of the history. */
  3833. rl_beginning_of_history ()
  3834. {
  3835.   rl_get_previous_history (1 + where_history ());
  3836. }
  3837.  
  3838. /* Meta-> goes to the end of the history.  (The current line). */
  3839. rl_end_of_history ()
  3840. {
  3841.   maybe_replace_line ();
  3842.   using_history ();
  3843.   maybe_unsave_line ();
  3844. }
  3845.  
  3846. /* Move down to the next history line. */
  3847. rl_get_next_history (count)
  3848.      int count;
  3849. {
  3850.   HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
  3851.  
  3852.   if (count < 0)
  3853.     {
  3854.       rl_get_previous_history (-count);
  3855.       return;
  3856.     }
  3857.  
  3858.   if (!count)
  3859.     return;
  3860.  
  3861.   maybe_replace_line ();
  3862.  
  3863.   while (count)
  3864.     {
  3865.       temp = next_history ();
  3866.       if (!temp)
  3867.     break;
  3868.       --count;
  3869.     }
  3870.  
  3871.   if (!temp)
  3872.     maybe_unsave_line ();
  3873.   else
  3874.     {
  3875.       strcpy (the_line, temp->line);
  3876.       rl_undo_list = (UNDO_LIST *)temp->data;
  3877.       rl_end = rl_point = strlen (the_line);
  3878.     }
  3879. }
  3880.  
  3881. /* Get the previous item out of our interactive history, making it the current
  3882.    line.  If there is no previous history, just ding. */
  3883. rl_get_previous_history (count)
  3884.      int count;
  3885. {
  3886.   HIST_ENTRY *old_temp = (HIST_ENTRY *)NULL;
  3887.   HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
  3888.  
  3889.   if (count < 0)
  3890.     {
  3891.       rl_get_next_history (-count);
  3892.       return;
  3893.     }
  3894.  
  3895.   if (!count)
  3896.     return;
  3897.  
  3898.   /* If we don't have a line saved, then save this one. */
  3899.   maybe_save_line ();
  3900.  
  3901.   /* If the current line has changed, save the changes. */
  3902.   maybe_replace_line ();
  3903.  
  3904.   while (count)
  3905.     {
  3906.       temp = previous_history ();
  3907.       if (!temp)
  3908.     break;
  3909.       else
  3910.     old_temp = temp;
  3911.       --count;
  3912.     }
  3913.  
  3914.   /* If there was a large argument, and we moved back to the start of the
  3915.      history, that is not an error.  So use the last value found. */
  3916.   if (!temp && old_temp)
  3917.     temp = old_temp;
  3918.  
  3919.   if (!temp)
  3920.     ding ();
  3921.   else
  3922.     {
  3923.       strcpy (the_line, temp->line);
  3924.       rl_undo_list = (UNDO_LIST *)temp->data;
  3925.       rl_end = rl_point = strlen (the_line);
  3926. #ifdef VI_MODE
  3927.       if (rl_editing_mode == vi_mode)
  3928.     rl_point = 0;
  3929. #endif /* VI_MODE */
  3930.     }
  3931. }
  3932.  
  3933. /* There is a command in ksh which yanks into this line, the last word
  3934.    of the previous line.  Here it is.  We left it on M-. */
  3935. rl_yank_previous_last_arg (ignore)
  3936.      int ignore;
  3937. {
  3938. }
  3939.  
  3940.  
  3941.  
  3942. /* **************************************************************** */
  3943. /*                                    */
  3944. /*            I-Search and Searching                */
  3945. /*                                    */
  3946. /* **************************************************************** */
  3947.  
  3948. /* Search backwards through the history looking for a string which is typed
  3949.    interactively.  Start with the current line. */
  3950. rl_reverse_search_history (sign, key)
  3951.      int sign;
  3952.      int key;
  3953. {
  3954.   rl_search_history (-sign, key);
  3955. }
  3956.  
  3957. /* Search forwards through the history looking for a string which is typed
  3958.    interactively.  Start with the current line. */
  3959. rl_forward_search_history (sign, key)
  3960.      int sign;
  3961.      int key;
  3962. {
  3963.   rl_search_history (sign, key);
  3964. }
  3965.  
  3966. /* Display the current state of the search in the echo-area.
  3967.    SEARCH_STRING contains the string that is being searched for,
  3968.    DIRECTION is zero for forward, or 1 for reverse,
  3969.    WHERE is the history list number of the current line.  If it is
  3970.    -1, then this line is the starting one. */
  3971. rl_display_search (search_string, reverse_p, where)
  3972.      char *search_string;
  3973.      int reverse_p, where;
  3974. {
  3975.   char *message = (char *)NULL;
  3976.  
  3977.   message =
  3978.     (char *)alloca (1 + (search_string ? strlen (search_string) : 0) + 30);
  3979.  
  3980.   *message = '\0';
  3981.  
  3982. #ifdef NEVER
  3983.   if (where != -1)
  3984.     sprintf (message, "[%d]", where + history_base);
  3985. #endif
  3986.  
  3987.   strcat (message, "(");
  3988.  
  3989.   if (reverse_p)
  3990.     strcat (message, "reverse-");
  3991.  
  3992.   strcat (message, "i-search)`");
  3993.  
  3994.   if (search_string)
  3995.     strcat (message, search_string);
  3996.  
  3997.   strcat (message, "': ");
  3998.   rl_message (message, 0, 0);
  3999.   rl_redisplay ();
  4000. }
  4001.  
  4002. /* Search through the history looking for an interactively typed string.
  4003.    This is analogous to i-search.  We start the search in the current line.
  4004.    DIRECTION is which direction to search; > 0 means forward, < 0 means
  4005.    backwards. */
  4006. rl_search_history (direction, invoking_key)
  4007.      int direction;
  4008.      int invoking_key;
  4009. {
  4010.   /* The string that the user types in to search for. */
  4011.   char *search_string = (char *)alloca (128);
  4012.  
  4013.   /* The current length of SEARCH_STRING. */
  4014.   int search_string_index;
  4015.  
  4016.   /* The list of lines to search through. */
  4017.   char **lines;
  4018.  
  4019.   /* The length of LINES. */
  4020.   int hlen;
  4021.  
  4022.   /* Where we get LINES from. */
  4023.   HIST_ENTRY **hlist = history_list ();
  4024.  
  4025.   int orig_point = rl_point;
  4026.   int orig_line = where_history ();
  4027.   int last_found_line = orig_line;
  4028.   int c, done = 0;
  4029.   register int i = 0;
  4030.  
  4031.  
  4032.   /* The line currently being searched. */
  4033.   char *sline;
  4034.  
  4035.   /* Offset in that line. */
  4036.   int index;
  4037.  
  4038.   /* Non-zero if we are doing a reverse search. */
  4039.   int reverse = (direction < 0);
  4040.  
  4041.   /* Create an arrary of pointers to the lines that we want to search. */
  4042.  
  4043.   maybe_replace_line ();
  4044.   if (hlist)
  4045.     for (i = 0; hlist[i]; i++);
  4046.  
  4047.   /* Allocate space for this many lines, +1 for the current input line,
  4048.      and remember those lines. */
  4049.   lines = (char **)alloca ((1 + (hlen = i)) * sizeof (char *));
  4050.   for (i = 0; i < hlen; i++)
  4051.     lines[i] = hlist[i]->line;
  4052.  
  4053.   if (saved_line_for_history)
  4054.     lines[i] = saved_line_for_history->line;
  4055.   else
  4056.     {
  4057.       /* So I have to type it in this way instead. */
  4058.       lines[i] = (char *)alloca (1 + strlen (the_line));
  4059.       strcpy (lines[i], &the_line[0]);
  4060.     }
  4061.  
  4062.   hlen++;
  4063.  
  4064.   /* The line where we start the search. */
  4065.   i = orig_line;
  4066.  
  4067.   /* Initialize search parameters. */
  4068.   *search_string = '\0';
  4069.   search_string_index = 0;
  4070.  
  4071.   rl_display_search (search_string, reverse, -1);
  4072.  
  4073.   sline = the_line;
  4074.   index = rl_point;
  4075.  
  4076.   while (!done)
  4077.     {
  4078.       c = rl_read_key ();
  4079.  
  4080.       /* Hack C to Do What I Mean. */
  4081.       {
  4082.     Function *f = (Function *)NULL;
  4083.  
  4084.     if (keymap[c].type == ISFUNC)
  4085.       f = keymap[c].function;
  4086.  
  4087.     if (f == rl_reverse_search_history)
  4088.       c = reverse ? -1 : -2;
  4089.     else if (f == rl_forward_search_history)
  4090.       c =  !reverse ? -1 : -2;
  4091.       }
  4092.  
  4093.       switch (c)
  4094.     {
  4095.     case ESC:
  4096.       done = 1;
  4097.       continue;
  4098.  
  4099.       /* case invoking_key: */
  4100.     case -1:
  4101.       goto search_again;
  4102.  
  4103.       /* switch directions */
  4104.     case -2:
  4105.       direction = -direction;
  4106.       reverse = (direction < 0);
  4107.  
  4108.       goto do_search;
  4109.  
  4110.     case CTRL ('G'):
  4111.       strcpy (the_line, lines[orig_line]);
  4112.       rl_point = orig_point;
  4113.       rl_end = strlen (the_line);
  4114.       rl_clear_message ();
  4115.       return;
  4116.  
  4117.     default:
  4118.       if (c < 32 || c > 126)
  4119.         {
  4120.           rl_execute_next (c);
  4121.           done = 1;
  4122.           continue;
  4123.         }
  4124.       else
  4125.         {
  4126.           search_string[search_string_index++] = c;
  4127.           search_string[search_string_index] = '\0';
  4128.           goto do_search;
  4129.  
  4130.         search_again:
  4131.  
  4132.           if (!search_string_index)
  4133.         continue;
  4134.           else
  4135.         {
  4136.           if (reverse)
  4137.             --index;
  4138.           else
  4139.             if (index != strlen (sline))
  4140.               ++index;
  4141.             else
  4142.               ding ();
  4143.         }
  4144.         do_search:
  4145.  
  4146.           while (1)
  4147.         {
  4148.           if (reverse)
  4149.             {
  4150.               while (index >= 0)
  4151.             if (strncmp
  4152.                 (search_string,
  4153.                  sline + index,
  4154.                  search_string_index) == 0)
  4155.               goto string_found;
  4156.             else
  4157.               index--;
  4158.             }
  4159.           else
  4160.             {
  4161.               register int limit =
  4162.             (strlen (sline) - search_string_index) + 1;
  4163.  
  4164.               while (index < limit)
  4165.             {
  4166.               if (strncmp (search_string,
  4167.                        sline + index,
  4168.                        search_string_index) == 0)
  4169.                 goto string_found;
  4170.               index++;
  4171.             }
  4172.             }
  4173.  
  4174.         next_line:
  4175.           i += direction;
  4176.  
  4177.           /* At limit for direction? */
  4178.           if ((reverse && i < 0) ||
  4179.               (!reverse && i == hlen))
  4180.             goto search_failed;
  4181.  
  4182.           sline = lines[i];
  4183.           if (reverse)
  4184.             index = strlen (sline);
  4185.           else
  4186.             index = 0;
  4187.  
  4188.           /* If the search string is longer than the current
  4189.              line, no match. */
  4190.           if (search_string_index > strlen (sline))
  4191.             goto next_line;
  4192.  
  4193.           /* Start actually searching. */
  4194.           if (reverse)
  4195.             index -= search_string_index;
  4196.         }
  4197.  
  4198.         search_failed:
  4199.           /* We cannot find the search string.  Ding the bell. */
  4200.           ding ();
  4201.           i = last_found_line;
  4202.           break;
  4203.  
  4204.         string_found:
  4205.           /* We have found the search string.  Just display it.  But don't
  4206.          actually move there in the history list until the user accepts
  4207.          the location. */
  4208.           strcpy (the_line, lines[i]);
  4209.           rl_point = index;
  4210.           rl_end = strlen (the_line);
  4211.           last_found_line = i;
  4212.           rl_display_search (search_string, reverse,
  4213.                  (i == orig_line) ? -1 : i);
  4214.         }
  4215.     }
  4216.       continue;
  4217.     }
  4218.   /* The user has won.  They found the string that they wanted.  Now all
  4219.      we have to do is place them there. */
  4220.   {
  4221.     int now = last_found_line;
  4222.  
  4223.     /* First put back the original state. */
  4224.     strcpy (the_line, lines[orig_line]);
  4225.  
  4226.     if (now < orig_line)
  4227.       rl_get_previous_history (orig_line - now);
  4228.     else
  4229.       rl_get_next_history (now - orig_line);
  4230.  
  4231.     rl_point = index;
  4232.     rl_clear_message ();
  4233.   }
  4234. }
  4235.  
  4236. /* Make C be the next command to be executed. */
  4237. rl_execute_next (c)
  4238.      int c;
  4239. {
  4240.   rl_pending_input = c;
  4241. }
  4242.  
  4243. /* **************************************************************** */
  4244. /*                                    */
  4245. /*            Killing Mechanism                */
  4246. /*                                    */
  4247. /* **************************************************************** */
  4248.  
  4249. /* What we assume for a max number of kills. */
  4250. #define DEFAULT_MAX_KILLS 10
  4251.  
  4252. /* The real variable to look at to find out when to flush kills. */
  4253. int rl_max_kills = DEFAULT_MAX_KILLS;
  4254.  
  4255. /* Where to store killed text. */
  4256. char **rl_kill_ring = (char **)NULL;
  4257.  
  4258. /* Where we are in the kill ring. */
  4259. int rl_kill_index = 0;
  4260.  
  4261. /* How many slots we have in the kill ring. */
  4262. int rl_kill_ring_length = 0;
  4263.  
  4264. /* How to say that you only want to save a certain amount
  4265.    of kill material. */
  4266. rl_set_retained_kills (num)
  4267.      int num;
  4268. {}
  4269.  
  4270. /* The way to kill something.  This appends or prepends to the last
  4271.    kill, if the last command was a kill command.  if FROM is less
  4272.    than TO, then the text is appended, otherwise prepended.  If the
  4273.    last command was not a kill command, then a new slot is made for
  4274.    this kill. */
  4275. rl_kill_text (from, to)
  4276.      int from, to;
  4277. {
  4278.   int slot;
  4279.   char *text = rl_copy (from, to);
  4280.  
  4281.   /* Is there anything to kill? */
  4282.   if (from == to) {
  4283.     free (text);
  4284.     last_command_was_kill++;
  4285.     return;
  4286.   }
  4287.  
  4288.   /* Delete the copied text from the line. */
  4289.   rl_delete_text (from, to);
  4290.  
  4291.   /* First, find the slot to work with. */
  4292.   if (!last_command_was_kill) {
  4293.  
  4294.     /* Get a new slot.  */
  4295.     if (!rl_kill_ring) {
  4296.  
  4297.       /* If we don't have any defined, then make one. */
  4298.       rl_kill_ring =
  4299.     (char **)xmalloc (((rl_kill_ring_length = 1) + 1) * sizeof (char *));
  4300.       slot = 1;
  4301.  
  4302.     } else {
  4303.  
  4304.       /* We have to add a new slot on the end, unless we have exceeded
  4305.      the max limit for remembering kills. */
  4306.       slot = rl_kill_ring_length;
  4307.       if (slot == rl_max_kills) {
  4308.     register int i;
  4309.     free (rl_kill_ring[0]);
  4310.     for (i = 0; i < slot; i++)
  4311.       rl_kill_ring[i] = rl_kill_ring[i + 1];
  4312.       } else {
  4313.     rl_kill_ring =
  4314.       (char **)xrealloc (rl_kill_ring,
  4315.                  ((slot = (rl_kill_ring_length += 1)) + 1)
  4316.                  * sizeof (char *));
  4317.       }
  4318.     }
  4319.     slot--;
  4320.   } else {
  4321.     slot = rl_kill_ring_length - 1;
  4322.   }
  4323.  
  4324.   /* If the last command was a kill, prepend or append. */
  4325.   if (last_command_was_kill) {
  4326.     char *old = rl_kill_ring[slot];
  4327.     char *new = (char *)xmalloc (1 + strlen (old) + strlen (text));
  4328.  
  4329.     if (from < to) {
  4330.       strcpy (new, old);
  4331.       strcat (new, text);
  4332.     } else {
  4333.       strcpy (new, text);
  4334.       strcat (new, old);
  4335.     }
  4336.     free (old);
  4337.     free (text);
  4338.     rl_kill_ring[slot] = new;
  4339.   } else {
  4340.     rl_kill_ring[slot] = text;
  4341.   }
  4342.   rl_kill_index = slot;
  4343.   last_command_was_kill++;
  4344. }
  4345.  
  4346. /* Now REMEMBER!  In order to do prepending or appending correctly, kill
  4347.    commands always make rl_point's original position be the FROM argument,
  4348.    and rl_point's extent be the TO argument. */
  4349.  
  4350.  
  4351. /* **************************************************************** */
  4352. /*                                    */
  4353. /*            Killing Commands                */
  4354. /*                                    */
  4355. /* **************************************************************** */
  4356.  
  4357. /* Delete the word at point, saving the text in the kill ring. */
  4358. rl_kill_word (count)
  4359.      int count;
  4360. {
  4361.   int orig_point = rl_point;
  4362.  
  4363.   if (count < 0)
  4364.     rl_backward_kill_word (-count);
  4365.   else
  4366.     {
  4367.       rl_forward_word (count);
  4368.  
  4369.       if (rl_point != orig_point)
  4370.     rl_kill_text (orig_point, rl_point);
  4371.  
  4372.       rl_point = orig_point;
  4373.     }
  4374. }
  4375.  
  4376. /* Rubout the word before point, placing it on the kill ring. */
  4377. rl_backward_kill_word (count)
  4378.      int count;
  4379. {
  4380.   int orig_point = rl_point;
  4381.  
  4382.   if (count < 0)
  4383.     rl_kill_word (-count);
  4384.   else
  4385.     {
  4386.       rl_backward_word (count);
  4387.  
  4388.       if (rl_point != orig_point)
  4389.     rl_kill_text (orig_point, rl_point);
  4390.     }
  4391. }
  4392.  
  4393. /* Kill from here to the end of the line.  If DIRECTION is negative, kill
  4394.    back to the line start instead. */
  4395. rl_kill_line (direction)
  4396.      int direction;
  4397. {
  4398.   int orig_point = rl_point;
  4399.  
  4400.   if (direction < 0)
  4401.     rl_backward_kill_line (1);
  4402.   else
  4403.     {
  4404.       rl_end_of_line ();
  4405.       if (orig_point != rl_point)
  4406.     rl_kill_text (orig_point, rl_point);
  4407.       rl_point = orig_point;
  4408.     }
  4409. }
  4410.  
  4411. /* Kill backwards to the start of the line.  If DIRECTION is negative, kill
  4412.    forwards to the line end instead. */
  4413. rl_backward_kill_line (direction)
  4414.      int direction;
  4415. {
  4416.   int orig_point = rl_point;
  4417.  
  4418.   if (direction < 0)
  4419.     rl_kill_line (1);
  4420.   else
  4421.     {
  4422.       if (!rl_point)
  4423.     ding ();
  4424.       else
  4425.     {
  4426.       rl_beg_of_line ();
  4427.       rl_kill_text (orig_point, rl_point);
  4428.     }
  4429.     }
  4430. }
  4431.  
  4432. /* Yank back the last killed text.  This ignores arguments. */
  4433. rl_yank ()
  4434. {
  4435.   if (!rl_kill_ring) rl_abort ();
  4436.   rl_insert_text (rl_kill_ring[rl_kill_index]);
  4437. }
  4438.  
  4439. /* If the last command was yank, or yank_pop, and the text just
  4440.    before point is identical to the current kill item, then
  4441.    delete that text from the line, rotate the index down, and
  4442.    yank back some other text. */
  4443. rl_yank_pop ()
  4444. {
  4445.   int l;
  4446.  
  4447.   if (((rl_last_func != rl_yank_pop) && (rl_last_func != rl_yank)) ||
  4448.       !rl_kill_ring)
  4449.     {
  4450.       rl_abort ();
  4451.     }
  4452.  
  4453.   l = strlen (rl_kill_ring[rl_kill_index]);
  4454.   if (((rl_point - l) >= 0) &&
  4455.       (strncmp (the_line + (rl_point - l),
  4456.         rl_kill_ring[rl_kill_index], l) == 0))
  4457.     {
  4458.       rl_delete_text ((rl_point - l), rl_point);
  4459.       rl_point -= l;
  4460.       rl_kill_index--;
  4461.       if (rl_kill_index < 0)
  4462.     rl_kill_index = rl_kill_ring_length - 1;
  4463.       rl_yank ();
  4464.     }
  4465.   else
  4466.     rl_abort ();
  4467.  
  4468. }
  4469.  
  4470. /* Yank the COUNTth argument from the previous history line. */
  4471. rl_yank_nth_arg (count, ignore)
  4472.      int count;
  4473. {
  4474.   register HIST_ENTRY *entry = previous_history ();
  4475.   char *arg;
  4476.  
  4477.   if (entry)
  4478.     next_history ();
  4479.   else
  4480.     {
  4481.       ding ();
  4482.       return;
  4483.     }
  4484.  
  4485.   arg = history_arg_extract (count, count, entry->line);
  4486.   if (!arg || !*arg)
  4487.     {
  4488.       ding ();
  4489.       return;
  4490.     }
  4491.  
  4492.   rl_begin_undo_group ();
  4493.   if (rl_point && the_line[rl_point - 1] != ' ')
  4494.     rl_insert_text (" ");
  4495.   rl_insert_text (arg);
  4496.   free (arg);
  4497.   rl_end_undo_group ();
  4498. }
  4499.  
  4500. /* Vi Mode. */
  4501. #ifdef VI_MODE
  4502. #include "vi_mode.c"
  4503. #endif /* VI_MODE */
  4504.  
  4505. /* How to toggle back and forth between editing modes. */
  4506. rl_vi_editing_mode ()
  4507. {
  4508. #ifdef VI_MODE
  4509.   rl_editing_mode = vi_mode;
  4510.   rl_vi_insertion_mode ();
  4511. #endif /* VI_MODE */
  4512. }
  4513.  
  4514. rl_emacs_editing_mode ()
  4515. {
  4516.   rl_editing_mode = emacs_mode;
  4517.   keymap = emacs_standard_keymap;
  4518. }
  4519.  
  4520.  
  4521. /* **************************************************************** */
  4522. /*                                    */
  4523. /*                 Completion                    */
  4524. /*                                    */
  4525. /* **************************************************************** */
  4526.  
  4527. /* Non-zero means that case is not significant in completion. */
  4528. int completion_case_fold = 0;
  4529.  
  4530. /* Return an array of (char *) which is a list of completions for TEXT.
  4531.    If there are no completions, return a NULL pointer.
  4532.    The first entry in the returned array is the substitution for TEXT.
  4533.     The remaining entries are the possible completions.
  4534.    The array is terminated with a NULL pointer.
  4535.  
  4536.    ENTRY_FUNCTION is a function of two args, and returns a (char *).
  4537.      The first argument is TEXT.
  4538.      The second is a state argument; it should be zero on the first call, and
  4539.      non-zero on subsequent calls.  It returns a NULL pointer to the caller
  4540.      when there are no more matches.
  4541.  */
  4542. char **
  4543. completion_matches (text, entry_function)
  4544.      char *text;
  4545.      char *(*entry_function) ();
  4546. {
  4547.   /* Number of slots in match_list. */
  4548.   int match_list_size;
  4549.  
  4550.   /* The list of matches. */
  4551.   char **match_list =
  4552.     (char **)xmalloc (((match_list_size = 10) + 1) * sizeof (char *));
  4553.  
  4554.   /* Number of matches actually found. */
  4555.   int matches = 0;
  4556.  
  4557.   /* Temporary string binder. */
  4558.   char *string;
  4559.  
  4560.   match_list[1] = (char *)NULL;
  4561.  
  4562.   while (string = (*entry_function) (text, matches))
  4563.     {
  4564.       if (matches + 1 == match_list_size)
  4565.     match_list =
  4566.       (char **)xrealloc (match_list,
  4567.                  ((match_list_size += 10) + 1) * sizeof (char *));
  4568.  
  4569.       match_list[++matches] = string;
  4570.       match_list[matches + 1] = (char *)NULL;
  4571.     }
  4572.  
  4573.   /* If there were any matches, then look through them finding out the
  4574.      lowest common denominator.  That then becomes match_list[0]. */
  4575.   if (matches)
  4576.     {
  4577.       register int i = 1;
  4578.       int low = 100000;        /* Count of max-matched characters. */
  4579.  
  4580.       /* If only one match, just use that. */
  4581.       if (matches == 1)
  4582.     {
  4583.       match_list[0] = match_list[1];
  4584.       match_list[1] = (char *)NULL;
  4585.     }
  4586.       else
  4587.     {
  4588.       /* Otherwise, compare each member of the list with
  4589.          the next, finding out where they stop matching. */
  4590.  
  4591.       while (i < matches)
  4592.         {
  4593.           register int c1, c2, si;
  4594.  
  4595.           if (completion_case_fold)
  4596.         {
  4597.           for (si = 0;
  4598.                (c1 = to_lower(match_list[i][si])) &&
  4599.                (c2 = to_lower(match_list[i + 1][si]));
  4600.                si++)
  4601.             if (c1 != c2) break;
  4602.         }
  4603.           else
  4604.         {
  4605.           for (si = 0;
  4606.                (c1 = match_list[i][si]) &&
  4607.                (c2 = match_list[i + 1][si]);
  4608.                si++)
  4609.             if (c1 != c2) break;
  4610.         }
  4611.  
  4612.           if (low > si) low = si;
  4613.           i++;
  4614.         }
  4615.       match_list[0] = (char *)xmalloc (low + 1);
  4616.       strncpy (match_list[0], match_list[1], low);
  4617.       match_list[0][low] = '\0';
  4618.     }
  4619.     }
  4620.   else                /* There were no matches. */
  4621.     {
  4622.       free (match_list);
  4623.       match_list = (char **)NULL;
  4624.     }
  4625.   return (match_list);
  4626. }
  4627.  
  4628. /* Okay, now we write the entry_function for filename completion.  In the
  4629.    general case.  Note that completion in the shell is a little different
  4630.    because of all the pathnames that must be followed when looking up the
  4631.    completion for a command. */
  4632. char *
  4633. filename_completion_function (text, state)
  4634.      int state;
  4635.      char *text;
  4636. {
  4637.   static DIR *directory;
  4638.   static char *filename = (char *)NULL;
  4639.   static char *dirname = (char *)NULL;
  4640.   static char *users_dirname = (char *)NULL;
  4641.   static int filename_len;
  4642.  
  4643.   struct direct *entry = (struct direct *)NULL;
  4644.  
  4645.   /* If we don't have any state, then do some initialization. */
  4646.   if (!state)
  4647.     {
  4648.       char *rindex (), *temp;
  4649.  
  4650.       if (dirname) free (dirname);
  4651.       if (filename) free (filename);
  4652.       if (users_dirname) free (users_dirname);
  4653.  
  4654.       filename = savestring (text);
  4655.       if (!*text) text = ".";
  4656.       dirname = savestring (text);
  4657.  
  4658.       temp = rindex (dirname, '/');
  4659.  
  4660.       if (temp)
  4661.     {
  4662.       strcpy (filename, ++temp);
  4663.       *temp = '\0';
  4664.     }
  4665.       else
  4666.     strcpy (dirname, ".");
  4667.  
  4668.       /* We aren't done yet.  We also support the "~user" syntax. */
  4669.  
  4670.       /* Save the version of the directory that the user typed. */
  4671.       users_dirname = savestring (dirname);
  4672.       {
  4673.     char *tilde_expand (), *temp_dirname = tilde_expand (dirname);
  4674.     free (dirname);
  4675.     dirname = temp_dirname;
  4676.  
  4677.     if (rl_symbolic_link_hook)
  4678.       (*rl_symbolic_link_hook) (&dirname);
  4679.       }
  4680.       directory = opendir (dirname);
  4681.       filename_len = strlen (filename);
  4682.  
  4683.       rl_filename_completion_desired = 1;
  4684.     }
  4685.  
  4686.   /* At this point we should entertain the possibility of hacking wildcarded
  4687.      filenames, like /usr/man*\/te<TAB>.  If the directory name contains
  4688.      globbing characters, then build an array of directories to glob on, and
  4689.      glob on the first one. */
  4690.  
  4691.   /* Now that we have some state, we can read the directory. */
  4692.  
  4693.   while (directory && (entry = readdir (directory)))
  4694.     {
  4695.       /* Special case for no filename.
  4696.      All entries except "." and ".." match. */
  4697.       if (!filename_len)
  4698.     {
  4699.       if ((strcmp (entry->d_name, ".") != 0) &&
  4700.           (strcmp (entry->d_name, "..") != 0))
  4701.         break;
  4702.     }
  4703.       else
  4704.     {
  4705.       /* Otherwise, if these match upto the length of filename, then
  4706.          it is a match. */
  4707. #ifdef TMB_SYSV
  4708.       if ((strlen (entry->d_name) >= filename_len) &&
  4709.           (strncmp (filename, entry->d_name, filename_len) == 0))
  4710. #else
  4711.         if ((entry->d_namlen >= filename_len) &&
  4712.         (strncmp (filename, entry->d_name, filename_len) == 0))
  4713. #endif /* TMB_SYSV */
  4714.           {
  4715.         break;
  4716.           }
  4717.     }
  4718.     }
  4719.  
  4720.   if (!entry)
  4721.     {
  4722.       if (directory)
  4723.     {
  4724.       closedir (directory);
  4725.       directory = (DIR *)NULL;
  4726.     }
  4727.       return (char *)NULL;
  4728.     }
  4729.   else
  4730.     {
  4731.       char *temp;
  4732.  
  4733.       if (dirname && (strcmp (dirname, ".") != 0))
  4734.     {
  4735. #ifdef TMB_SYSV
  4736.       temp = (char *)xmalloc (1 + strlen (users_dirname)
  4737.                   + strlen (entry->d_name));
  4738. #else
  4739.       temp = (char *)xmalloc (1 + strlen (users_dirname)
  4740.                   + entry->d_namlen);
  4741. #endif /* TMB_SYSV */
  4742.       strcpy (temp, users_dirname);
  4743.       strcat (temp, entry->d_name);
  4744.     }
  4745.       else
  4746.     {
  4747.       temp = (savestring (entry->d_name));
  4748.     }
  4749.       return (temp);
  4750.     }
  4751. }
  4752.  
  4753.  
  4754. /* **************************************************************** */
  4755. /*                                    */
  4756. /*            Binding keys                    */
  4757. /*                                    */
  4758. /* **************************************************************** */
  4759.  
  4760. /* rl_add_defun (char *name, Function *function, int key)
  4761.    Add NAME to the list of named functions.  Make FUNCTION
  4762.    be the function that gets called.
  4763.    If KEY is not -1, then bind it. */
  4764. rl_add_defun (name, function, key)
  4765.      char *name;
  4766.      Function *function;
  4767.      int key;
  4768. {
  4769.   if (key != -1)
  4770.     rl_bind_key (key, function);
  4771.   rl_add_funmap_entry (name, function);
  4772. }
  4773.  
  4774. /* Bind KEY to FUNCTION.  Returns non-zero if KEY is out of range. */
  4775. int
  4776. rl_bind_key (key, function)
  4777.      int key;
  4778.      Function *function;
  4779. {
  4780.   if (key < 0)
  4781.     return (key);
  4782.  
  4783.   if (key > 127 && key < 256)
  4784.     {
  4785.       if (keymap[ESC].type == ISKMAP)
  4786.     {
  4787.       Keymap escmap = (Keymap)keymap[ESC].function;
  4788.  
  4789.       key -= 128;
  4790.       escmap[key].type = ISFUNC;
  4791.       escmap[key].function = function;
  4792.       return (0);
  4793.     }
  4794.       return (key);
  4795.     }
  4796.  
  4797.   keymap[key].type = ISFUNC;
  4798.   keymap[key].function = function;
  4799.  return (0);
  4800. }
  4801.  
  4802. /* Bind KEY to FUNCTION in MAP.  Returns non-zero in case of invalid
  4803.    KEY. */
  4804. int
  4805. rl_bind_key_in_map (key, function, map)
  4806.      int key;
  4807.      Function *function;
  4808.      Keymap map;
  4809. {
  4810.   int result;
  4811.   Keymap oldmap = keymap;
  4812.  
  4813.   keymap = map;
  4814.   result = rl_bind_key (key, function);
  4815.   keymap = oldmap;
  4816.   return (result);
  4817. }
  4818.  
  4819. /* Make KEY do nothing in the currently selected keymap.
  4820.    Returns non-zero in case of error. */
  4821. int
  4822. rl_unbind_key (key)
  4823.      int key;
  4824. {
  4825.   return (rl_bind_key (key, (Function *)NULL));
  4826. }
  4827.  
  4828. /* Make KEY do nothing in MAP.
  4829.    Returns non-zero in case of error. */
  4830. int
  4831. rl_unbind_key_in_map (key, map)
  4832.      int key;
  4833.      Keymap map;
  4834. {
  4835.   return (rl_bind_key_in_map (key, (Function *)NULL, map));
  4836. }
  4837.  
  4838. /* Bind the key sequence represented by the string KEYSEQ to
  4839.    FUNCTION.  This makes new keymaps as necessary.  The initial
  4840.    place to do bindings is in MAP. */
  4841. rl_set_key (keyseq, function, map)
  4842.      char *keyseq;
  4843.      Function *function;
  4844.      Keymap map;
  4845. {
  4846.   rl_generic_bind (ISFUNC, keyseq, function, map);
  4847. }
  4848.  
  4849. /* Bind the key sequence represented by the string KEYSEQ to
  4850.    the string of characters MACRO.  This makes new keymaps as
  4851.    necessary.  The initial place to do bindings is in MAP. */
  4852. rl_macro_bind (keyseq, macro, map)
  4853.      char *keyseq, *macro;
  4854.      Keymap map;
  4855. {
  4856.   char *macro_keys = (char *)xmalloc (2 * (strlen (macro)));
  4857.   int macro_keys_len;
  4858.  
  4859.   if (rl_translate_keyseq (macro, macro_keys, ¯o_keys_len))
  4860.     {
  4861.       free (macro_keys);
  4862.       return;
  4863.     }
  4864.   rl_generic_bind (ISMACR, keyseq, macro_keys, map);
  4865. }
  4866.  
  4867. /* Bind the key sequence represented by the string KEYSEQ to
  4868.    the arbitrary pointer DATA.  TYPE says what kind of data is
  4869.    pointed to by DATA, right now this can be a function (ISFUNC),
  4870.    a macro (ISMACR), or a keymap (ISKMAP).  This makes new keymaps
  4871.    as necessary.  The initial place to do bindings is in MAP. */
  4872. rl_generic_bind (type, keyseq, data, map)
  4873.      int type;
  4874.      char *keyseq, *data;
  4875.      Keymap map;
  4876. {
  4877.   char *keys;
  4878.   int keys_len;
  4879.   register int i;
  4880.  
  4881.   /* If no keys to bind to, exit right away. */
  4882.   if (!keyseq || !*keyseq)
  4883.     {
  4884.       if (type == ISMACR)
  4885.     free (data);
  4886.       return;
  4887.     }
  4888.  
  4889.   keys = (char *)alloca (1 + (2 * strlen (keyseq)));
  4890.  
  4891.   /* Translate the ASCII representation of KEYSEQ into an array
  4892.      of characters.  Stuff the characters into ARRAY, and the
  4893.      length of ARRAY into LENGTH. */
  4894.   if (rl_translate_keyseq (keyseq, keys, &keys_len))
  4895.     return;
  4896.  
  4897.   /* Bind keys, making new keymaps as necessary. */
  4898.   for (i = 0; i < keys_len; i++)
  4899.     {
  4900.       if (i + 1 < keys_len)
  4901.     {
  4902.       if (map[keys[i]].type != ISKMAP)
  4903.         {
  4904.           if (map[i].type == ISMACR)
  4905.         free ((char *)map[i].function);
  4906.  
  4907.           map[keys[i]].type = ISKMAP;
  4908.           map[keys[i]].function = (Function *)rl_make_bare_keymap ();
  4909.         }
  4910.       map = (Keymap)map[keys[i]].function;
  4911.     }
  4912.       else
  4913.     {
  4914.       if (map[keys[i]].type == ISMACR)
  4915.         free ((char *)map[keys[i]].function);
  4916.  
  4917.       map[keys[i]].function = (Function *)data;
  4918.       map[keys[i]].type = type;
  4919.     }
  4920.     }
  4921. }
  4922.  
  4923. /* Translate the ASCII representation of SEQ, stuffing the
  4924.    values into ARRAY, an array of characters.  LEN gets the
  4925.    final length of ARRAY.  Return non-zero if there was an
  4926.    error parsing SEQ. */
  4927. rl_translate_keyseq (seq, array, len)
  4928.      char *seq, *array;
  4929.      int *len;
  4930. {
  4931.   register int i, c, l = 0;
  4932.  
  4933.   for (i = 0; c = seq[i]; i++)
  4934.     {
  4935.       if (c == '\\')
  4936.     {
  4937.       c = seq[++i];
  4938.  
  4939.       if (!c)
  4940.         break;
  4941.  
  4942.       if (((c == 'C' || c == 'M') &&  seq[i + 1] == '-') ||
  4943.           (c == 'e'))
  4944.         {
  4945.           /* Handle special case of backwards define. */
  4946.           if (strncmp (&seq[i], "C-\\M-", 5) == 0)
  4947.         {
  4948.           array[l++] = ESC;
  4949.           i += 5;
  4950.           array[l++] = CTRL (to_upper (seq[i]));
  4951.           if (!seq[i])
  4952.             i--;
  4953.           continue;
  4954.         }
  4955.  
  4956.           switch (c)
  4957.         {
  4958.         case 'M':
  4959.           i++;
  4960.           array[l++] = ESC;
  4961.           break;
  4962.  
  4963.         case 'C':
  4964.           i += 2;
  4965.           array[l++] = CTRL (to_upper (seq[i]));
  4966.           break;
  4967.  
  4968.         case 'e':
  4969.           array[l++] = ESC;
  4970.         }
  4971.  
  4972.           continue;
  4973.         }
  4974.     }
  4975.       array[l++] = c;
  4976.     }
  4977.  
  4978.   *len = l;
  4979.   array[l] = '\0';
  4980.   return (0);
  4981. }
  4982.  
  4983. /* Return a pointer to the function that STRING represents.
  4984.    If STRING doesn't have a matching function, then a NULL pointer
  4985.    is returned. */
  4986. Function *
  4987. rl_named_function (string)
  4988.      char *string;
  4989. {
  4990.   register int i;
  4991.  
  4992.   for (i = 0; funmap[i]; i++)
  4993.     if (stricmp (funmap[i]->name, string) == 0)
  4994.       return (funmap[i]->function);
  4995.   return ((Function *)NULL);
  4996. }
  4997.  
  4998. /* The last key bindings file read. */
  4999. static char *last_readline_init_file = "~/.inputrc";
  5000.  
  5001. /* Re-read the current keybindings file. */
  5002. rl_re_read_init_file (count, ignore)
  5003.      int count, ignore;
  5004. {
  5005.   rl_read_init_file (last_readline_init_file);
  5006. }
  5007.  
  5008. /* Do key bindings from a file.  If FILENAME is NULL it defaults
  5009.    to `~/.inputrc'.  If the file existed and could be opened and
  5010.    read, 0 is returned, otherwise errno is returned. */
  5011. int
  5012. rl_read_init_file (filename)
  5013.      char *filename;
  5014. {
  5015.   extern int errno;
  5016.   int line_size, line_index;
  5017.   char *line = (char *)xmalloc (line_size = 100);
  5018.   char *openname;
  5019.   FILE *file;
  5020.  
  5021.   int c;
  5022.  
  5023.   /* Default the filename. */
  5024.   if (!filename)
  5025.     filename = "~/.inputrc";
  5026.  
  5027.   openname = tilde_expand (filename);
  5028.  
  5029.   /* Open the file. */
  5030.   file = fopen (openname, "r");
  5031.   free (openname);
  5032.  
  5033.   if (!file)
  5034.     return (errno);
  5035.  
  5036.   last_readline_init_file = filename;
  5037.  
  5038.   /* Loop reading lines from the file.  Lines that start with `#' are
  5039.      comments, all other lines are commands for readline initialization. */
  5040.   while ((c = rl_getc (file)) != EOF)
  5041.     {
  5042.       /* If comment, flush to EOL. */
  5043.       if (c == '#')
  5044.     {
  5045.       while ((c = rl_getc (file)) != EOF && c != '\n');
  5046.       if (c == EOF)
  5047.         goto function_exit;
  5048.       continue;
  5049.     }
  5050.  
  5051.       /* Otherwise, this is the start of a line.  Read the
  5052.      line from the file. */
  5053.       line_index = 0;
  5054.       while (c != EOF && c != '\n')
  5055.     {
  5056.       line[line_index++] = c;
  5057.       if (line_index == line_size)
  5058.         line = (char *)xrealloc (line, line_size += 100);
  5059.       c = rl_getc (file);
  5060.     }
  5061.       line[line_index] = '\0';
  5062.  
  5063.       /* Parse the line. */
  5064.       rl_parse_and_bind (line);
  5065.     }
  5066.  
  5067. function_exit:
  5068.  
  5069.   free (line);
  5070.   /* Close up the file and exit. */
  5071.   fclose (file);
  5072.   return (0);
  5073. }
  5074.  
  5075.  
  5076. /* **************************************************************** */
  5077. /*                                    */
  5078. /*            Parser Directives                   */
  5079. /*                                    */
  5080. /* **************************************************************** */
  5081.  
  5082. /* Conditionals. */
  5083.  
  5084. /* Calling programs set this to have their argv[0]. */
  5085. char *rl_readline_name = "other";
  5086.  
  5087. /* Stack of previous values of parsing_conditionalized_out. */
  5088. static unsigned char *if_stack = (unsigned char *)NULL;
  5089. static int if_stack_depth = 0;
  5090. static int if_stack_size = 0;
  5091.  
  5092. /* Push parsing_conditionalized_out, and set parser state based on ARGS. */
  5093. parser_if (args)
  5094.      char *args;
  5095. {
  5096.   register int i;
  5097.  
  5098.   /* Push parser state. */
  5099.   if (if_stack_depth + 1 >= if_stack_size)
  5100.     {
  5101.       if (!if_stack)
  5102.     if_stack = (unsigned char *)xmalloc (if_stack_size = 20);
  5103.       else
  5104.     if_stack = (unsigned char *)xrealloc (if_stack, if_stack_size += 20);
  5105.     }
  5106.   if_stack[if_stack_depth++] = parsing_conditionalized_out;
  5107.  
  5108.   /* We only check to see if the first word in ARGS is the same as the
  5109.      value stored in rl_readline_name. */
  5110.  
  5111.   /* Isolate first argument. */
  5112.   for (i = 0; args[i] && !whitespace (args[i]); i++);
  5113.  
  5114.   if (args[i])
  5115.     args[i++] = '\0';
  5116.  
  5117.   if (stricmp (args, rl_readline_name) == 0)
  5118.     parsing_conditionalized_out = 0;
  5119.   else
  5120.     parsing_conditionalized_out = 1;
  5121. }
  5122.  
  5123. /* Invert the current parser state if there is anything on the stack. */
  5124. parser_else (args)
  5125.      char *args;
  5126. {
  5127.   if (if_stack_depth)
  5128.     parsing_conditionalized_out = !parsing_conditionalized_out;
  5129.   else
  5130.     {
  5131.       /* *** What, no error message? *** */
  5132.     }
  5133. }
  5134.  
  5135. /* Terminate a conditional, popping the value of
  5136.    parsing_conditionalized_out from the stack. */
  5137. parser_endif (args)
  5138.      char *args;
  5139. {
  5140.   if (if_stack_depth)
  5141.     parsing_conditionalized_out = if_stack[--if_stack_depth];
  5142.   else
  5143.     {
  5144.       /* *** What, no error message? *** */
  5145.     }
  5146. }
  5147.  
  5148. /* Associate textual names with actual functions. */
  5149. static struct {
  5150.   char *name;
  5151.   Function *function;
  5152. } parser_directives [] = {
  5153.   { "if", parser_if },
  5154.   { "endif", parser_endif },
  5155.   { "else", parser_else },
  5156.   { (char *)0x0, (Function *)0x0 }
  5157. };
  5158.  
  5159. /* Handle a parser directive.  STATEMENT is the line of the directive
  5160.    without any leading `$'. */
  5161. static int
  5162. handle_parser_directive (statement)
  5163.      char *statement;
  5164. {
  5165.   register int i;
  5166.   char *directive, *args;
  5167.  
  5168.   /* Isolate the actual directive. */
  5169.  
  5170.   /* Skip whitespace. */
  5171.   for (i = 0; whitespace (statement[i]); i++);
  5172.  
  5173.   directive = &statement[i];
  5174.  
  5175.   for (; statement[i] && !whitespace (statement[i]); i++);
  5176.  
  5177.   if (statement[i])
  5178.     statement[i++] = '\0';
  5179.  
  5180.   for (; statement[i] && whitespace (statement[i]); i++);
  5181.  
  5182.   args = &statement[i];
  5183.  
  5184.   /* Lookup the command, and act on it. */
  5185.   for (i = 0; parser_directives[i].name; i++)
  5186.     if (stricmp (directive, parser_directives[i].name) == 0)
  5187.       {
  5188.     (*parser_directives[i].function) (args);
  5189.     return (0);
  5190.       }
  5191.  
  5192.   /* *** Should an error message be output? */
  5193.   return (1);
  5194. }
  5195.  
  5196. /* Read the binding command from STRING and perform it.
  5197.    A key binding command looks like: Keyname: function-name\0,
  5198.    a variable binding command looks like: set variable value.
  5199.    A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
  5200. rl_parse_and_bind (string)
  5201.      char *string;
  5202. {
  5203.   extern char *possible_control_prefixes[], *possible_meta_prefixes[];
  5204.   char *rindex (), *funname, *kname;
  5205.   static int substring_member_of_array ();
  5206.   register int c;
  5207.   int key, i;
  5208.  
  5209.   if (!string || !*string || *string == '#')
  5210.     return;
  5211.  
  5212.   /* If this is a parser directive, act on it. */
  5213.   if (*string == '$')
  5214.     {
  5215.       handle_parser_directive (&string[1]);
  5216.       return;
  5217.     }
  5218.  
  5219.   /* If we are supposed to be skipping parsing right now, then do it. */
  5220.   if (parsing_conditionalized_out)
  5221.     return;
  5222.  
  5223.   i = 0;
  5224.   /* If this keyname is a complex key expression surrounded by quotes,
  5225.      advance to after the matching close quote. */
  5226.   if (*string == '"')
  5227.     {
  5228.       for (i = 1; c = string[i]; i++)
  5229.     {
  5230.       if (c == '"' && string[i - 1] != '\\')
  5231.         break;
  5232.     }
  5233.     }
  5234.  
  5235.   /* Advance to the colon (:) or whitespace which separates the two objects. */
  5236.   for (; (c = string[i]) && c != ':' && c != ' ' && c != '\t'; i++ );
  5237.  
  5238.   /* Mark the end of the command (or keyname). */
  5239.   if (string[i])
  5240.     string[i++] = '\0';
  5241.  
  5242.   /* If this is a command to set a variable, then do that. */
  5243.   if (stricmp (string, "set") == 0)
  5244.     {
  5245.       char *var = string + i;
  5246.       char *value;
  5247.  
  5248.       /* Make VAR point to start of variable name. */
  5249.       while (*var && whitespace (*var)) var++;
  5250.  
  5251.       /* Make value point to start of value string. */
  5252.       value = var;
  5253.       while (*value && !whitespace (*value)) value++;
  5254.       if (*value)
  5255.     *value++ = '\0';
  5256.       while (*value && whitespace (*value)) value++;
  5257.  
  5258.       rl_variable_bind (var, value);
  5259.       return;
  5260.     }
  5261.  
  5262.   /* Skip any whitespace between keyname and funname. */
  5263.   for (; string[i] && whitespace (string[i]); i++);
  5264.   funname = &string[i];
  5265.  
  5266.   /* Now isolate funname.
  5267.      For straight function names just look for whitespace, since
  5268.      that will signify the end of the string.  But this could be a
  5269.      macro definition.  In that case, the string is quoted, so skip
  5270.      to the matching delimiter. */
  5271.   if (*funname == '\'' || *funname == '"')
  5272.     {
  5273.       int delimiter = string[i++];
  5274.  
  5275.       for (; c = string[i]; i++)
  5276.     {
  5277.       if (c == delimiter && string[i - 1] != '\\')
  5278.         break;
  5279.     }
  5280.       if (c)
  5281.     i++;
  5282.     }
  5283.  
  5284.   /* Advance to the end of the string.  */
  5285.   for (; string[i] && !whitespace (string[i]); i++);
  5286.  
  5287.   /* No extra whitespace at the end of the string. */
  5288.   string[i] = '\0';
  5289.  
  5290.   /* If this is a new-style key-binding, then do the binding with
  5291.      rl_set_key ().  Otherwise, let the older code deal with it. */
  5292.   if (*string == '"')
  5293.     {
  5294.       char *seq = (char *)alloca (1 + strlen (string));
  5295.       register int j, k = 0;
  5296.  
  5297.       for (j = 1; string[j]; j++)
  5298.     {
  5299.       if (string[j] == '"' && string[j - 1] != '\\')
  5300.         break;
  5301.  
  5302.       seq[k++] = string[j];
  5303.     }
  5304.       seq[k] = '\0';
  5305.  
  5306.       /* Binding macro? */
  5307.       if (*funname == '\'' || *funname == '"')
  5308.     {
  5309.       j = strlen (funname);
  5310.  
  5311.       if (j && funname[j - 1] == *funname)
  5312.         funname[j - 1] = '\0';
  5313.  
  5314.       rl_macro_bind (seq, &funname[1], keymap);
  5315.     }
  5316.       else
  5317.     rl_set_key (seq, rl_named_function (funname), keymap);
  5318.  
  5319.       return;
  5320.     }
  5321.  
  5322.   /* Get the actual character we want to deal with. */
  5323.   kname = rindex (string, '-');
  5324.   if (!kname)
  5325.     kname = string;
  5326.   else
  5327.     kname++;
  5328.  
  5329.   key = glean_key_from_name (kname);
  5330.  
  5331.   /* Add in control and meta bits. */
  5332.   if (substring_member_of_array (string, possible_control_prefixes))
  5333.     key = CTRL (to_upper (key));
  5334.  
  5335.   if (substring_member_of_array (string, possible_meta_prefixes))
  5336.     key = META (key);
  5337.  
  5338.   /* Temporary.  Handle old-style keyname with macro-binding. */
  5339.   if (*funname == '\'' || *funname == '"')
  5340.     {
  5341.       char seq[2];
  5342.       int fl = strlen (funname);
  5343.  
  5344.       seq[0] = key; seq[1] = '\0';
  5345.       if (fl && funname[fl - 1] == *funname)
  5346.     funname[fl - 1] = '\0';
  5347.  
  5348.       rl_macro_bind (seq, &funname[1], keymap);
  5349.     }
  5350.   else
  5351.     rl_bind_key (key, rl_named_function (funname));
  5352. }
  5353.  
  5354. rl_variable_bind (name, value)
  5355.      char *name, *value;
  5356. {
  5357.   if (stricmp (name, "editing-mode") == 0)
  5358.     {
  5359.       if (strnicmp (value, "vi", 2) == 0)
  5360.     {
  5361. #ifdef VI_MODE
  5362.       keymap = vi_insertion_keymap;
  5363.       rl_editing_mode = vi_mode;
  5364. #endif /* VI_MODE */
  5365.     }
  5366.       else if (strnicmp (value, "emacs", 5) == 0)
  5367.     {
  5368.       keymap = emacs_standard_keymap;
  5369.       rl_editing_mode = emacs_mode;
  5370.     }
  5371.     }
  5372.   else if (stricmp (name, "horizontal-scroll-mode") == 0)
  5373.     {
  5374.       if (!*value || stricmp (value, "On") == 0)
  5375.     horizontal_scroll_mode = 1;
  5376.       else
  5377.     horizontal_scroll_mode = 0;
  5378.     }
  5379.   else if (stricmp (name, "mark-modified-lines") == 0)
  5380.     {
  5381.       if (!*value || stricmp (value, "On") == 0)
  5382.     mark_modified_lines = 1;
  5383.       else
  5384.     mark_modified_lines = 0;
  5385.     }
  5386. }
  5387.  
  5388. /* Return the character which matches NAME.
  5389.    For example, `Space' returns ' '. */
  5390.  
  5391. typedef struct {
  5392.   char *name;
  5393.   int value;
  5394. } assoc_list;
  5395.  
  5396. assoc_list name_key_alist[] = {
  5397.   { "Space", ' ' },
  5398.   { "SPC", ' ' },
  5399.   { "Rubout", 0x7f },
  5400.   { "DEL", 0x7f },
  5401.   { "Tab", 0x09 },
  5402.   { "Newline", '\n' },
  5403.   { "Return", '\r' },
  5404.   { "RET", '\r' },
  5405.   { "LFD", '\n' },
  5406.   { "Escape", '\033' },
  5407.   { "ESC", '\033' },
  5408.  
  5409.   { (char *)0x0, 0 }
  5410. };
  5411.  
  5412. int
  5413. glean_key_from_name (name)
  5414.      char *name;
  5415. {
  5416.   register int i;
  5417.  
  5418.   for (i = 0; name_key_alist[i].name; i++)
  5419.     if (stricmp (name, name_key_alist[i].name) == 0)
  5420.       return (name_key_alist[i].value);
  5421.  
  5422.   return (*name);
  5423. }
  5424.  
  5425.  
  5426. /* **************************************************************** */
  5427. /*                                    */
  5428. /*            String Utility Functions            */
  5429. /*                                    */
  5430. /* **************************************************************** */
  5431.  
  5432. /* Return non-zero if any members of ARRAY are a substring in STRING. */
  5433. static int
  5434. substring_member_of_array (string, array)
  5435.      char *string, **array;
  5436. {
  5437.   static char *strindex ();
  5438.  
  5439.   while (*array)
  5440.     {
  5441.       if (strindex (string, *array))
  5442.     return (1);
  5443.       array++;
  5444.     }
  5445.   return (0);
  5446. }
  5447.  
  5448. /* Whoops, Unix doesn't have strnicmp. */
  5449.  
  5450. /* Compare at most COUNT characters from string1 to string2.  Case
  5451.    doesn't matter. */
  5452. static int
  5453. strnicmp (string1, string2, count)
  5454.      char *string1, *string2;
  5455. {
  5456.   register char ch1, ch2;
  5457.  
  5458.   while (count)
  5459.     {
  5460.       ch1 = *string1++;
  5461.       ch2 = *string2++;
  5462.       if (to_upper(ch1) == to_upper(ch2))
  5463.     count--;
  5464.       else break;
  5465.     }
  5466.   return (count);
  5467. }
  5468.  
  5469. /* strcmp (), but caseless. */
  5470. static int
  5471. stricmp (string1, string2)
  5472.      char *string1, *string2;
  5473. {
  5474.   register char ch1, ch2;
  5475.  
  5476.   while (*string1 && *string2)
  5477.     {
  5478.       ch1 = *string1++;
  5479.       ch2 = *string2++;
  5480.       if (to_upper(ch1) != to_upper(ch2))
  5481.     return (1);
  5482.     }
  5483.   return (*string1 | *string2);
  5484. }
  5485.  
  5486. /* Determine if s2 occurs in s1.  If so, return a pointer to the
  5487.    match in s1.  The compare is case insensitive. */
  5488. static char *
  5489. strindex (s1, s2)
  5490.      register char *s1, *s2;
  5491. {
  5492.   register int i, l = strlen (s2);
  5493.   register int len = strlen (s1);
  5494.  
  5495.   for (i = 0; (len - i) >= l; i++)
  5496.     if (strnicmp (&s1[i], s2, l) == 0)
  5497.       return (s1 + i);
  5498.   return ((char *)NULL);
  5499. }
  5500.  
  5501.  
  5502. /* **************************************************************** */
  5503. /*                                    */
  5504. /*            SYSV Support                    */
  5505. /*                                    */
  5506. /* **************************************************************** */
  5507.  
  5508. /* Since system V reads input differently than we do, I have to
  5509.    make a special version of getc for that. */
  5510.  
  5511. #ifdef SYSV
  5512.  
  5513. extern int errno;
  5514. #include <sys/errno.h>
  5515.  
  5516. int
  5517. rl_getc (stream)
  5518.      FILE *stream;
  5519. {
  5520.   int result;
  5521.   unsigned char c;
  5522.  
  5523.   while (1)
  5524.     {
  5525.       result = read (fileno (stream), &c, sizeof (char));
  5526.       if (result == sizeof (char))
  5527.     return (c);
  5528.  
  5529.       if (errno != EINTR)
  5530.     return (EOF);
  5531.     }
  5532. }
  5533. #else
  5534. int
  5535. rl_getc (stream)
  5536.      FILE *stream;
  5537. {
  5538.   return (getc (stream));
  5539. }
  5540. #endif
  5541.  
  5542. #ifdef STATIC_MALLOC
  5543.  
  5544. /* **************************************************************** */
  5545. /*                                    */
  5546. /*            xmalloc and xrealloc ()                     */
  5547. /*                                    */
  5548. /* **************************************************************** */
  5549.  
  5550. static void memory_error_and_abort ();
  5551.  
  5552. static char *
  5553. xmalloc (bytes)
  5554.      int bytes;
  5555. {
  5556.   char *temp = (char *)malloc (bytes);
  5557.  
  5558.   if (!temp)
  5559.     memory_error_and_abort ();
  5560.   return (temp);
  5561. }
  5562.  
  5563. static char *
  5564. xrealloc (pointer, bytes)
  5565.      char *pointer;
  5566.      int bytes;
  5567. {
  5568.   char *temp = (char *)realloc (pointer, bytes);
  5569.  
  5570.   if (!temp)
  5571.     memory_error_and_abort ();
  5572.   return (temp);
  5573. }
  5574.  
  5575. static void
  5576. memory_error_and_abort ()
  5577. {
  5578.   fprintf (stderr, "readline: Out of virtual memory!\n");
  5579.   abort ();
  5580. }
  5581. #endif /* STATIC_MALLOC */
  5582.  
  5583.  
  5584. /* **************************************************************** */
  5585. /*                                    */
  5586. /*            Testing Readline                */
  5587. /*                                    */
  5588. /* **************************************************************** */
  5589.  
  5590. #ifdef TEST
  5591.  
  5592. main ()
  5593. {
  5594.   HIST_ENTRY **history_list ();
  5595.   char *temp = (char *)NULL;
  5596.   char *prompt = "readline% ";
  5597.   int done = 0;
  5598.  
  5599.   while (!done)
  5600.     {
  5601.       temp = readline (prompt);
  5602.  
  5603.       /* Test for EOF. */
  5604.       if (!temp)
  5605.     exit (1);
  5606.  
  5607.       /* If there is anything on the line, print it and remember it. */
  5608.       if (*temp)
  5609.     {
  5610.       fprintf (stderr, "%s\r\n", temp);
  5611.       add_history (temp);
  5612.     }
  5613.  
  5614.       /* Check for `command' that we handle. */
  5615.       if (strcmp (temp, "quit") == 0)
  5616.     done = 1;
  5617.  
  5618.       if (strcmp (temp, "list") == 0) {
  5619.     HIST_ENTRY **list = history_list ();
  5620.     register int i;
  5621.     if (list) {
  5622.       for (i = 0; list[i]; i++) {
  5623.         fprintf (stderr, "%d: %s\r\n", i, list[i]->line);
  5624.         free (list[i]->line);
  5625.       }
  5626.       free (list);
  5627.     }
  5628.       }
  5629.       free (temp);
  5630.     }
  5631. }
  5632.  
  5633. #endif /* TEST */
  5634.  
  5635.  
  5636. /*
  5637.  * Local variables:
  5638.  * compile-command: "gcc -g -traditional -I. -I.. -DTEST -o readline readline.c keymaps.o funmap.o history.o -ltermcap"
  5639.  * end:
  5640.  */
  5641.